Hacker News new | more | comments | ask | show | jobs | submit login
“Do What I Mean”: Name Resolution in Programming Languages (willcrichton.net)
36 points by wcrichton 5 months ago | hide | past | web | favorite | 8 comments

Variable shadowing should be an error. If you have to answer the "which X is this" questions the author covers when reading code, the language is too confusing. Better to get a compile time error.

I disagree. Shadowing can be very useful when transforming types that represent the same thing. For example, rebinding a variable to be immutable.

Another one is traversing enums in Rust: https://docs.rs/syn/0.15/syn/enum.Expr.html#syntax-tree-enum...

Those are examples of why shadowing is bad. Too-cute use of a risky feature. When looking for usage of a variable by search, it's helpful that there not be shadowing. Cuteness means trouble for maintenance programmers.

> Imagine that function A binds the variable FOO, and calls the function B, which calls the function C, and C uses the value of FOO.

Languages that lack dynamic scoping typically propagate a reference to a "context" through the B function. One can see this common pattern in threading libraries such as pthreads, for example. The A function creates a data structure that contains everything C needs, passes it to B as the "context" parameter and it is then forwarded to C.

It's more verbose but also more explicit.

While the opinions of Mssrs. Steele and Stallman should not be lightly dismissed, and I think I can see where they are coming from, the marketplace of programming languages seems to have voted against dynamic scoping to an extent that it has not, for example, chosen definitively for or against static typing. Dynamically-scoped variables are rather like globals, but with the added complication that each one is a stack.

Concrete solutions to Stallman's objection in lexically scoped languages include,

- Callable objects that store `FOO`, and

- Closures that capture `FOO`.

I'm pretty leery of argumentum ad populum. I think we went with lexical scoping for a few clear reasons, and I don't know if the reasons for dynamic scoping are compelling. I would love to hear more of them, though.

My comparison of dynamically scoped variables to globals might be wrong, but, unlike my comment on the way languages have developed, it is not an argumentum ad populum.

It has been a while since I worked with dynamically-scoped programs, but I found it harder to reason about how they worked. Maybe it was just because I was less familiar with the technique.

It might be that all forms of beyond-local scoping multiply the difficulty in reasoning about programs (especially in the presence of mutable data), and dynamic scoping is a more powerful form of it, increasing the opportunity for setting up difficult-to-follow situations.

The issue of ease of understanding is a different one than ease of extending, and I would judge the former to be more important, all else being equal.

Applications are open for YC Summer 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact