You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I recently bumped into CoffeeScript Madness which reminded me of the counterintuitive behavior in CoffeeScript (and autoVar/autoLet that mimic it) that assigning a variable in an outer scope can modify the behavior of functions defined (deep) within that scope. There are also counterarguments. In practice I've found it a convenient but very dangerous feature.
Scoping was changed in other languages like CokeScript and LiveScript, which both use := to mean "assign this variable from an outer scope" (just CoffeeScript's =), and otherwise make new vars in each function scope (unlike CoffeeScript). Obviously we can't use := in Civet though.
I've long been thinking of adding a flag like "civet autoVar shadow" that automatically makes a var at each function scope, but allows some way to escape and access variables at a higher scope. What came to my mind was Python's nonlocal keyword, or maybe another name like outer, as in:
"civet autoVar shadow"x=5functionincrement()
outer x
x++functionmyOwnX()
x=10
↓↓↓
var x
x=5functionincrement()
return x++functionmyOwnX()
var x
returnx=10
I'm not sure what's best, though, so I thought I'd open it up to discussion here.
proposalProposal or discussion about a significant language feature
1 participant
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I recently bumped into CoffeeScript Madness which reminded me of the counterintuitive behavior in CoffeeScript (and
autoVar
/autoLet
that mimic it) that assigning a variable in an outer scope can modify the behavior of functions defined (deep) within that scope. There are also counterarguments. In practice I've found it a convenient but very dangerous feature.Scoping was changed in other languages like CokeScript and LiveScript, which both use
:=
to mean "assign this variable from an outer scope" (just CoffeeScript's=
), and otherwise make newvar
s in each function scope (unlike CoffeeScript). Obviously we can't use:=
in Civet though.I've long been thinking of adding a flag like
"civet autoVar shadow"
that automatically makes avar
at each function scope, but allows some way to escape and access variables at a higher scope. What came to my mind was Python'snonlocal
keyword, or maybe another name likeouter
, as in:I'm not sure what's best, though, so I thought I'd open it up to discussion here.
Beta Was this translation helpful? Give feedback.
All reactions