top | item 40133869

(no title)

jbjohns | 1 year ago

https://github.com/Chalarangelo/30-seconds-of-interviews/blo...

Exception handling, for example, uses dynamic scoping since you don't know what will be handling your exception when you write code which throws it.

Another way of thinking about it is, with dynamic scoping the value of the dynamic variable must always be on the stack and the closest one is the value that will be used. This is a really good behaviour for global variables since a common source of bugs is some global variables (and I'm considering class members "global" for this) getting changed unexpectedly. If the variable is lexical then it can be very hard to figure out what changed the value (especially when threads are involved) but if the variable is dynamic it's easy: the culprit is in the stack trace.

discuss

order

codethief|1 year ago

Just noticed your comment now: Thanks so much for the explanation! However, my question was more about the blog post in particular, i.e. coroutines (async/await) vs. effect handlers: I still fail to see why coroutines are lexically scoped. async/await yield control back to the calling function which might or might not define an event loop or yield control to another function further up the stack. How is this different from handling effects or exceptions? Whose scope does "lexically scoped" refer to here?

EDIT: Ah, reading this comment[0],

> The more fundamental difference between effects and coroutines is that a `yield` in a coroutine always goes to the one unique resumer

maybe I thought too much of Python where async/await are implemented via generators and, unless I'm mistaken, there need not be a unique resumer/event loop.

[0]: https://news.ycombinator.com/item?id=40108636