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

mutable variables are not considered local, thus cannot be evaluated in conditionals #154

Open
nohehf opened this issue May 30, 2024 · 1 comment

Comments

@nohehf
Copy link

nohehf commented May 30, 2024

The following tsg stanza, while executed on an empty python file:

(module) {
    var a = #true
    if a {
        print "a is true"
    }
}

Will error:

Expected local value at (6, 8)
test.tsg:6:8:
6 |     if a {
  |        ^

Where it would work just fine with an immutable "let":

(module) {
    let a = #true
    if a {
        print "a is true"
    }
}

Will print "a is true" as expected.

This is a weird behavior, as a should have the same scope in both cases. It also prevents to do patterns such as:

var a = ; some expression that might be null
if (is-null a) {
    set a = ; some default value
}

Additionally I think that it is quite limiting not being able to execute conditionals on non-local values, why is that ?

@hendrikvanantwerpen
Copy link
Collaborator

The analysis could indeed be more precise around mutable variables. It might require changing the structure of the checker a little. I think it's currently a single-pass thing, and that wouldn't work anymore. The checks would have to be moved to the end of a block.

Additionally I think that it is quite limiting not being able to execute conditionals on non-local values, why is that ?

Two for keeping control local to a stanza reasons here:

  • It makes reasoning about stanza behavior much easier. The shape of the graph that is created by a stanza is completely determined by the nodes that were matched.

  • It keeps the implementation simpler. Stanza's can be reduced to graph statements and expression thunks that can be lazily evaluation. That ensures that stanza evaluation order doesn't matter. If we wanted to allow conditionals on non-local values, we'd have to push allt he control logic into the lazy expressions. It's possible, but the implementation becomes quite complex.

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

2 participants