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

Intersection types #46

Merged
merged 2 commits into from
Jun 6, 2018
Merged

Intersection types #46

merged 2 commits into from
Jun 6, 2018

Conversation

Simn
Copy link
Member

@Simn Simn commented May 17, 2018

Support Type1 & Type2 intersection type syntax.

Rendered version

@ncannasse
Copy link
Member

I'm curious whereas & is the correct syntax here. & is about intersection whereas | is about union. But here is's more an union of all fields than the intersection (common denominator) of them.
Also, it's a quite short syntax for something that's in the end quite rarely used (I think?).

Another option would be to use extends keyword instead of &

@back2dos
Copy link
Member

The intersection of two structures is the structure containing the union of their fields. I tried to explain this here: HaxeFoundation/haxe#7006 (comment)

@Simn
Copy link
Member Author

Simn commented May 18, 2018

Funny how we both get confused by the terminology. I think this always initially confuses people who tend to think "in rectangles".

The misconception is that the rectangles are assumed to be the fields, and their intersection would then be their common fields. However, the correct interpretation is that the rectangles are (compatible/assignable/unifyable) types, and the intersection is then the types which are (compatible with/assignable to/unifyable with) every type making up the intersection.

Conversely, the union of two structure types would be those types that can be assigned to type 1 or (not xor) type 2, i.e. types that have the fields of one or (|) the other structure.

All this is a natural consequence of the fact that we allow assigning smaller structures to larger ones.

Note that I don't want to make the terminology of intersection types very prevalent. It would likely only show up in the error message and the ComplexType constructor name. For all intents and purposes, this feature works like > X, > Y and :(A, B) did before, so it shouldn't confuse users who don't develop type-systems. As a bonus, there's a sound theory behind it.

I have updated the proposal to mention future possibilities regarding interfaces.

@skial skial mentioned this pull request May 24, 2018
1 task
@sledorze
Copy link

sledorze commented May 24, 2018

I can't emphasis enough to take a look at Typescript definitions of unions / intersections / etc..
Merely because I guess everybody will want to derive Haxe externs automatically from those Typescript definitions to cover all Web APIs.

@ncannasse
Copy link
Member

ok for me

@nadako
Copy link
Member

nadako commented Jun 4, 2018

I find intersection types and unions useful in some situations, and while of course this proposal's functionality is a bit limited, it doesn't seem to restrict future enhancements, so let's do it! \o/

@Simn Simn merged commit 8d2b106 into HaxeFoundation:master Jun 6, 2018
@Simn Simn deleted the intersection-types branch June 6, 2018 14:38
Gama11 added a commit to vshaxe/haxe-TmLanguage that referenced this pull request Jun 9, 2018
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.

5 participants