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

Disallow non-invariant types in Pointer#value= #7225

Closed

Conversation

asterite
Copy link
Member

Fixes #6997

The code in #6997 shouldn't compile at all, as I explained in the issue.

This PR changes Pointer#value= to disallow non-invariant types. If you have a Pointer(Array(String | Int32)) then you must insert an Array(String | Int32) into it. You can't insert Array(String) or Array(Int32).

As a second step to this I would like to change all restrictions to always check invariance for generic types. Right now we can do x : Range(Int, Int) as a type restriction to say "any type that inherits Int" but that gives the problem of #6997. We can change the semantic to mean "must be an Int" (in this case it doesn't make sense) and eventually introduce some syntax to mean "must be an Int or inherit it".

@asterite
Copy link
Member Author

We should probably not merge this because it breaks for tuples. That is, one should be able to do:

ptr = Pointer(Tuple(Int32 | String)).malloc(1)
ptr.value = {1}

That's because the compiler automatically expands "smaller" tuples to "bigger" tuples. But it doesn't do that for other types. So... we should change the matching semantic: for tuples it's fine to be covariant, for other types it's not.

@asterite
Copy link
Member Author

(or we can remove that extra tuple logic, it's probably better to simplify the language)

@asterite asterite closed this Dec 28, 2018
@j8r
Copy link
Contributor

j8r commented Dec 28, 2018

@asterite I guess this compiles:

ptr = Pointer(Tuple(Int32 | String)).malloc(1)
ptr.value = {1.as(Int32 | String)}

I'm not sure Tuple with unions are as used as for Array

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

Successfully merging this pull request may close these issues.

2 participants