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

Assert doesn't erase nilable when used in branch tail... #79

Open
jcorbin opened this issue Nov 23, 2019 · 1 comment
Open

Assert doesn't erase nilable when used in branch tail... #79

jcorbin opened this issue Nov 23, 2019 · 1 comment

Comments

@jcorbin
Copy link
Contributor

jcorbin commented Nov 23, 2019

Rather it only seems to work when it's in the main branch.

This fails:

class such:
    x = 0

var much = nil

if not much:
    much = such {}
    assert much

much.x++
print(much)

much.x++
print(much)

With foo.lobster(10): error: type such? does not have field: x

But hoisting the assert much works.

This has been a contrived example, reduced down from something like:

  • a class with a nil-able resource? field
  • where I load it the first time through, something like if not tex: load()
  • where there's a def load() method that ends with an assert tex after having loaded from a file
  • I ended up just inlining the loader method, and hoisting its guard assertion into main control flow...
@aardappel
Copy link
Owner

Yeah, there's currently no flow information going out of scopes. If there was, the assert wouldn't even be necessary, since much = such {} already makes it non-nil.

But yeah, this would require interesting logic, since now we need to basically say, both "branches" of the if come to the same conclusion for a certain variable, so that information can be merged. That would be a new kind of construct in the typechecker.

I guess I haven't run into this much, since the nil checking just makes me religiously try to avoid any nils in my code :)

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