-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Union types #1887
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
[RFC] Union types #1887
Conversation
This is trivial to disallow again, by removing the special case in compiler
Unsure if this should be 0 or 1 but it needs to be there
do it |
I don't know what core developers think! |
@sharifzadesina you tell 'em m8 |
I followed the internal discussion but I did not understand the main reason for voting overwhelmingly in favor of no, this feature seems nevertheless important following the latest changes caused by type hints. Someone to enlighten me ? |
man that's so much work :( . Hopefully later on devs will decide to implement it. |
Closing as the RFC has been declined (for now!) |
@lilty I do not like this proposal and I think it will bring just more confusion. When you have a type hint declaration you can understand what can you do with a value. For example if you have an But what if you have Accepting or returning values of multiple types is generally a bad idea that makes code more complicated and prone to errors. Regarding standart PHP functions like |
I would vote "no" as well because of the purposed syntax. There are a lot of problems:
IMHO the cleaner implementation would be the way it's done in Java - method-overloading. You will be able to have a method with the same name multiple times, but each method has to accept different arguments types. This will make the code cleaner and easier to work with and debug. If the purposed syntax was implemented the way it is - it would have made a lot of people's life a living hell. @codedokode that's why you should have the ability to do method overloading - use the same method name and implement the code to handle the different cases. It's easier to debug and you end up having cleaner code. Furthermore, your IDE would know exactly which method you're trying to call by looking at the types of arguments you're passing and will show you "hints". This has already been done - we don't need to re-invent the wheel. :) |
Overloading has problems of its own too. For example in PHP some types can be automatically converted (e.g. number to string when concatenating). Also PHP is not completely statically typed so it will not always be clear what version of method would be called. |
@nikic Can you point the link to where the discussion happened which decided to decline this proposal? |
This RFC needs to be revisited for PHP 8. |
Ping for this. Intersection types are useful for middleware containers, where the container's content can be described as an intersection of different interfaces, e.g. Also described here: https://phpixie.com/blog/taking-middleware-psr-7-and-mvc-to-the-next-level.html |
This is the patch for union and intersection types, see https://wiki.php.net/rfc/union_types
It currently allows
| null
in an union, which is very simple to remove again from compiler, in case we decide to vote first on the RFC without support for null.