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
omake claims to implement static scope for private variables, but actually doesn't do that. It seems that private variables behave much like dynamic ones, except that at the entry of a function the environment is restored.
Example 1: This code should not work, but works:
fun1() =
private.x = foo
export x
fun2() =
private.r = $(fun1)
value $(x)
println($(fun2)) # prints "foo"
It is possible to export the variable to a different function although it isn't visible there via static scoping rules.
Example 2: The other problematic aspect is that updates in the environment are not pushed to the recorded environment in the closure:
private.n = 0
fun1() =
n = $(add $n, 1)
export n
fun1()
fun1()
println($(n)) # prints 1 instead of 2
When fun1 is defined, the interpreter records the current static environment, containing n=0. Whenever fun1 is called, it will operate on this environment and return an update of that.
This can only be fixed by switching to a completely different implementation strategy (e.g. by allowing direct mutation of the static environment instead of passing updated environments from context to context). Figuring this out will take some time. Until then, we should disallow problematic uses of private variables:
Exporting private variables from a function should be forbidden
When exporting a private variable from a scope we need to check whether it already exists in the target scope, and if not report an error
Basically, this means that private variables are intended to be used only inside a function for local values. (And we should also make function parameters private by default - so far these are variables with dynamic scope.)
Example 3: Note that this will continue to print the wrong value even with the additional checks:
private.n = 0
fun1() =
println($(n))
n = 5
fun1() # prints 0 instead of 5
This is not fixable at the moment.
The text was updated successfully, but these errors were encountered:
omake claims to implement static scope for private variables, but actually doesn't do that. It seems that private variables behave much like dynamic ones, except that at the entry of a function the environment is restored.
Example 1: This code should not work, but works:
It is possible to export the variable to a different function although it isn't visible there via static scoping rules.
Example 2: The other problematic aspect is that updates in the environment are not pushed to the recorded environment in the closure:
When
fun1
is defined, the interpreter records the current static environment, containingn=0
. Wheneverfun1
is called, it will operate on this environment and return an update of that.This can only be fixed by switching to a completely different implementation strategy (e.g. by allowing direct mutation of the static environment instead of passing updated environments from context to context). Figuring this out will take some time. Until then, we should disallow problematic uses of private variables:
Basically, this means that private variables are intended to be used only inside a function for local values. (And we should also make function parameters private by default - so far these are variables with dynamic scope.)
Example 3: Note that this will continue to print the wrong value even with the additional checks:
This is not fixable at the moment.
The text was updated successfully, but these errors were encountered: