HaxeFoundation / haxe-evolution

Repository for maintaining proposal for changes to the Haxe programming language
111 stars 58 forks source link

Intersection types #46

Closed Simn closed 6 years ago

Simn commented 6 years ago

Support Type1 & Type2 intersection type syntax.

Rendered version

ncannasse commented 6 years ago

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 commented 6 years ago

The intersection of two structures is the structure containing the union of their fields. I tried to explain this here: https://github.com/HaxeFoundation/haxe/issues/7006#issuecomment-386996261

Simn commented 6 years ago

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.

sledorze commented 6 years ago

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 commented 6 years ago

ok for me

nadako commented 6 years ago

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/