Referencing local variable before assignment results in unexpected behavior

It’s certainly not the defining characteristic of an inner function — that’s just that the function definition is syntactically inside the body of another function, there’s no semantic implication (to me). It could be completely separate from the outer function or it could be a closure. The defining characteristic of a closure is that it “closes over” outer local variables. That means that if you read an outer local from a closure, it will have the binding it had in the outer scope. In a language that allows reassignment of locals at all — which Clojure does not — if you reassign an outer local from a closure, it changes the outer binding. Since Clojure doesn’t allow reassignment of locals at all, its inner functions are closures in this sense. If it allowed local assignment but assignment from an inner function was disallowed or did something different like creating a new local, that would be a different story, but it’s not allowed at all.

Again, in purely functional languages that don’t allow reassignment of locals, there’s no way to distinguish capture by reference or value, so they cannot possibly argue for one or the other. The logical structure is that some people are saying “if I do A then B should happen” while others are saying “if I do A then C should happen”. The pure functional languages are not saying either of these things: they are saying “we don’t allow A in the first place”. You cannot say that this position in in favor of B or C — it’s equally in favor of and against both since it’s against A being possible at all.

3 Likes