-
Notifications
You must be signed in to change notification settings - Fork 9
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
Add a Collection
type
#40
base: master
Are you sure you want to change the base?
Conversation
fe4de0c
to
6bb3dde
Compare
83caec5
to
a813982
Compare
6bb3dde
to
a9dc526
Compare
#50 has been merged, but this is not entirely obsolete. In some cases the typechecker expects a collection, it needs to handle that. And also, when it extracts an element type from a collection, now it’s no longer sufficient to match on the type, we have to look inside the union for candidates too. |
d717a8c
to
4398e41
Compare
I want to add an unpack operator, and to be able to type the type expectation, I need a collection supertype for List and Set. Maybe once I have this, I can actually type the union operator in a more elegant way as well and I no longer need unpack. But unpack seems nice anyway, and a Collection supertype seems nice as well.
It already worked before, but we would only report the mismatch afterwards, and not blame inside, maybe even insert a runtime check. Propagating the expecation inside results in better errors and more efficient code.
At first I wasn't sure if I need to add Collection here, and how to report an inner mismatch. But now it's clear, the TypeDiff should match the expected supertype.
It would be nice to have that, but the current implementation is not incorrect, so let's do the easy thing for now, we can always make it better later.
Note to self, previously I wanted to add
That would depend on whether
Though now that I’m writing this, maybe it is not that hard to just support |
I am not really happy with this, because the type is not really something that end-users should use (although they can), it's more for the typechecker internally. How to explain this nicely?
Background
I want to add an unpack operator, so you can write
[..xs]
instead of[for x in xs: x]
, possibly to replace the union operator too. But to be able to propagate type expectations into collection literals, I need a way to express “List[T]
orSet[T]
”.Solution
Defines a new type,
Collection[T]
. In the lattice, it is the meet ofList[T]
andSet[T]
. I have a feeling that it will make typechecking the union operator more elegant as well, so maybe we can keep|
after all. The new type fits in very naturally to the existing typechecking machinery, which is some evidence that the current iteration of #26 is a good approach.To do