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

Type narrowing for struct members #70

Open
Zettelkasten opened this issue Dec 23, 2021 · 0 comments
Open

Type narrowing for struct members #70

Zettelkasten opened this issue Dec 23, 2021 · 0 comments

Comments

@Zettelkasten
Copy link
Owner

I.e. if you have a type struct Foo { mem: Int|Str },
then this should work:

a = Foo(42)
print(a.mem + 5)
a.mem = "Hello world!"
print(a.mem[:5])

One way to implement this would be to simply change the narrowed types of the variables, e.g. when a is first declared as Foo { mem: 0:Int|1:Str }, we could narrow it to Foo { mem: 0:Int } or Foo { mem: 1|Str }.
For this to work we need to change how struct identities work (needed for self-referencing struct members): Currently structs are equal, if they have the same struct identity and the same template parameters.
But Foo should not be equal to the Foo where the members have been narrowed.
Naively we would compare the members for equality, but if done wrong, this leads to an infinite cycle when there is a self-referencing member.

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