PHP Internals News: Episode 88: Pure Intersection Types
PHP Internals News: Episode 88: Pure Intersection Types
In this episode of "PHP Internals News" I talk with George Peter Banyard (Website, Twitter, GitHub, GitLab) about the "Pure Intersection Types" RFC that he has proposed.
The RSS feed for this podcast is https://derickrethans.nl/feed-phpinternalsnews.xml, you can download this episode's MP3 file, and it's available on Spotify and iTunes. There is a dedicated website: https://phpinternals.news
Transcript
- Derick Rethans 0:14
-
Welcome to PHP internals news, a podcast dedicated to explaining the latest developments in the PHP language. This is Episode 88. Today I'm talking with George Peter Banyard about pure intersection types. George, could you please introduce yourself?
- George Peter Banyard 0:30
-
Hello, my name is George Peter Banyard. I work on PHP code development in my free time. And on the PHP Docs.
- Derick Rethans 0:36
-
This RFC is about intersection types. What are intersection types?
- George Peter Banyard 0:40
-
I think the easiest way to explain intersection types is to use something which we already have, which are union types. So union types tells you I want X or Y, whereas intersection types tell you that I want X and Y to be true at the same time. The easiest example I can come up with is a traversable that you want to be countable as well. So traversable and countable. Currently, you can do intersection types in very hacky ways. So you can either create a new interface which extends both traversable and countable, but then all the classes that you want to be using this fashion, you need to make them implement the interface, which might not be possible if you using a library or other things like that. The other very hacky way of doing it is using reference and typed properties. You assign two typed properties by reference, one being traversable, one being countable, and then your actual property, you type alias reference it, with both of these properties. And then my PHP will check: does the property respect type A those reference? If yes, move to the next one. It doesn't respect type B, which basically gives you intersection types.
- Derick Rethans 1:44
-
Yeah, I saw that in the RFC. And I was wondering like, well, people actually do that?
- George Peter Banyard 1:49
-
The only reason I know that is because of Nikita's slide.
- Derick Rethans 1:51
-
The thing is, if it is possible, people will do it, right. And that's how that works.
- George Peter Banyard 1:56
-
Yeah, most of the times.
- Derick Rethans 1:57
-
The RFC isn't actually called intersection types. It's called pure intersection types. What does the word pure do here?
- George Peter Banyard 2:05
-
So the word pure here is not very semantic. But it's more that you cannot mix union types and intersection types together. The reasons for it are mostly technical. One reason is how do you mix and match intersection types and union types? One way is to have like union types take precedence over intersection types, but some people don't like that and want to explicit it grouping all the time. So you need to do parentheses, A intersection B, close parentheses, pipe for the union, and then the other type. But I think the main reason is mostly the variance, like the variance checks for inheritance are already kind of complicated and kind of mind boggling.
- Derick Rethans 2:44
Truncated by Planet PHP, read more at the original (another 20384 bytes)