abs icon indicating copy to clipboard operation
abs copied to clipboard

Allow functions to write to their inherited scope

Open odino opened this issue 4 years ago • 6 comments

⧐  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...

odino avatar May 24 '20 15:05 odino

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...

odino avatar May 24 '20 15:05 odino

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)

gromgit avatar Aug 02 '20 02:08 gromgit

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

odino avatar Aug 02 '20 18:08 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).

gromgit avatar Aug 03 '20 04:08 gromgit

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 :)

odino avatar Aug 05 '20 19:08 odino

In that case, I recommend local-by-default semantics (it makes reasoning easier), and global <varname>... to override.

gromgit avatar Aug 06 '20 05:08 gromgit