abs
abs copied to clipboard
Allow functions to write to their inherited scope
⧐ x = 1
⧐ f test() { x = x + 1 }
f test() {x = (x + 1);}
⧐ test()
⧐ test()
⧐ test()
⧐ x
1
Though I'm not really sure this is a good idea...
Here is a rough implementation: https://github.com/abs-lang/abs/commit/126b7c6ebdd7bb69649552f2f8138670a54563b3
But I think I wanna hold off as I don't think it makes a lot of sense...
IMO, the current scoping rules of ABS are quite confusing. Perhaps a future iteration could switch to one of the following:
- Bash-style: All function linkages are external unless scoped with
local
- Tcl-style: All function linkages are local unless scoped with
global
orupvar
(Tcl is the only language I know where you can actually specify the number of "stack frames" to cross when creating variable references)
What do you find confusing?
On Sun, Aug 2, 2020 at 6:50 AM Adrian Ho [email protected] wrote:
IMO, the current scoping rules of ABS are quite confusing. Perhaps a future iteration could switch to one of the following:
- Bash-style: All function linkages are external unless scoped with local
- Tcl-style: All function linkages are local unless scoped with global or upvar (Tcl is the only language I know where you can actually specify the number of "stack frames" to cross when creating variable references)
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/abs-lang/abs/issues/373#issuecomment-667616938, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACQFZBSXM2QT4NQIMFYHT3R6TH5ZANCNFSM4NI5YG6Q .
-- Alex Nadalin http://www.linkedin.com/in/alessandronadalin www.odino.org www.twitter.com/odino
What do you find confusing?
The fact that your example function:
f test() { x = x + 1 }
changes the scope of x
midway, from global (RHS) to local (LHS).
I agree it's a bit weird since in a reasonable environment you'd get an error for that:
>>> x = 1
>>> def test():
... x = x + 1
...
>>> test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in test
UnboundLocalError: local variable 'x' referenced before assignment
I'm down to revisit this :)
In that case, I recommend local-by-default semantics (it makes reasoning easier), and global <varname>...
to override.