by magicalhippo · 2y ago · 0 comments
Having a context argument which is passed to the callback has been par for the course in all cases I've seen, but I've mostly been in C land and related areas.
Though, if you have closures, should you still prefer a context param?
> If JavaScript had this feature, the syntax for defining and calling functions would be the same, no matter whether you're dealing with a normal function, a generator, or an async function.
How would one reason about performance and possibly concurrency in such languages?
It's ambient state, just like a `catch` block, and so you can make it ambiently available to a whole tree of function calls. The outcome is the same as passing around a context argument by hand, and it can have equally strong type-checking, but the compiler handles all of the boilerplate for you.
To answer your second question: I agree that, in a dynamic language like JavaScript, making all of these language features implicit could be too costly. I'm more optimistic about effect systems in statically-typed languages.
So a "localized global variable" of sorts? Sounds interesting, got any links to examples I could look at?
It isn't a very common language feature, but you could take a look at React's `Context` or Common Lisp's `defparameter`.
I was imagining more along the lines of (on-the-spot pseudo-code)
using [x := foo(...)]
{
bar();
output();
}
func bar()
{
x = x + 1
}
where bar() and output(), or something they call in turn, can reference x and it would be a compile-time error to use bar() or output() in cases where they couldn't resolve x. Or something more along those lines.Anyway, cheers, always fun to ponder these things.