Referencing local variable before assignment results in unexpected behavior

Yeah, you are right. The word “inner” is purely a syntactic property. It does not specify the concept I wanted to bring up. I probably should’ve said value-or-variable-capturing-maybe-stateful-inner-function or something. I don’t really know a good word for this…

I think I understand this and I thought I could bring up a different point.

I was bringing up Clojure as an example language that defaults to “capture by value” where “capture by reference” is optionally possible (i.e., it can do Scheme-like impure “functional” programming), even though it’s a functional language focusing on immutability. I thought it was interesting because Rust came to a somewhat similar design when approaching from the side of imperative programming; i.e., you need to annotate the closure with mut. It’s a convergence of the design from both directions.

Using your analysis of the logical structure, I think the question is more about “which one should be the default, A => B or A => C?” Julia, Clojure, Rust, Python, etc. all let you write (roughly speaking) two types of inner functions: (1) variable/object updates in it are observable by the outer function and (2) the values defined in the outer function are “copied” to the inner function. I think the main difference is the default behavior, rather than which one is possible (unless you are using Haskell).

6 Likes