I wish #JavaScript has dynamic scopes as like in Perl or Emacs Lisp or contextvars (context-local states) as like in Python.
-
洪 民憙 (Hong Minhee)replied to 洪 民憙 (Hong Minhee) last edited by
I might be able to implement half-baked dynamic scopes using global variables, but you know, it won't work well with concurrent contexts.
-
@hongminhee I'm not sure what you're needing these for, but it sounds like the trials and tribulations of the Node.js Domains API.
-
🍄🌈🎮💻🚲🥓🎃💀🏴🛻🇺🇸replied to 洪 民憙 (Hong Minhee) last edited by
@hongminhee why?
-
@[email protected]
func.bind(...)
나func.apply(...)
로 함수 컨텍스트의 this를 바꿔치기해서 비슷한 걸 해볼 수 있을 것 같은데요 (다만 arrow function은 lexical this를 사용하므로 안 먹힘), 제가 보기엔 뭔가 뭔가... 모르겠습니다 -
@thisismissem Oh, it's quite interesting! Although the docs says it's pending deprecation…
Anyway, you could get the idea by looking at Python's contextvars.
contextvars — Context Variables
This module provides APIs to manage, store, and access context-local state. The ContextVar class is used to declare and work with Context Variables. The copy_context() function and the Context clas...
Python documentation (docs.python.org)
-
@tirr 여러가지 궁리를 해보고 있는데 브라우저 환경까지 생각하면 일단은 불가능한 게 아닌가 잠정적으로 생각하고 있습니다…
-
洪 民憙 (Hong Minhee)replied to 🍄🌈🎮💻🚲🥓🎃💀🏴🛻🇺🇸 last edited by
@schizanon Well, for the same reason why Python has contextvars? I don't want to add the same parameter to every function in my app codebase.
-
@hongminhee maybe AsyncLocalStorage? https://docs.adonisjs.com/guides/concepts/async-local-storage
-
洪 民憙 (Hong Minhee)replied to Emelia 👸🏻 last edited by
@thisismissem Yeah, I found it from Node.js API, and Deno apparently implements it too, but web browsers don't have such thing… There's a proposal though:
GitHub - tc39/proposal-async-context: Async Context for JavaScript
Async Context for JavaScript. Contribute to tc39/proposal-async-context development by creating an account on GitHub.
GitHub (github.com)
-
@hongminhee Have you considered functional maps (e.g. HAMT)? Whenever you add a "variable binding" (key value pair) you get a new instance with this binding added, but don't modify the original data structure.