Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

private variables are broken #43

Open
gerdstolpmann opened this issue Sep 11, 2016 · 1 comment
Open

private variables are broken #43

gerdstolpmann opened this issue Sep 11, 2016 · 1 comment

Comments

@gerdstolpmann
Copy link
Collaborator

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.

@gerdstolpmann
Copy link
Collaborator Author

Added some remarks to section 5.1 of the reference manual, describing the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant