-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add support for the mixed type #5313
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
Conversation
This needs tests for how |
Yeah, I know. It was just too late when I finished this. 😊 |
Zend/tests/type_declarations/mixed/syntax/mixed_parameter_error.phpt
Outdated
Show resolved
Hide resolved
@nikic I've just added the missing variance tests :) I believe only "auxiliary" stuffs like reflection tests needs to be added now, but correct me if I'm wrong. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kocsismate Have you consider the type any
instead of mixed
?
I have a feeling that this name will be clearer, and we don't have the bias of the famous "mixed and no return type, what is the difference"
?
Zend/tests/type_declarations/mixed/inheritance/mixed_return_inheritance_success4.phpt
Show resolved
Hide resolved
5febb2f
to
ad7e93a
Compare
Just to double check, under this implementation |
@nikic We can say yes.. Although there is one more exception: return types, in which case the |
I see, so that also means that you can't go |
I guess that's fine. Adding a return type always requires it to be specified on inheriting methods as well, so nothing new here. @kocsismate You might want to avoid generating a VERIFY_RETURN opcode in case of mixed type. |
@nikic Thanks for the hint with the opcode! We'll come up with an RFC first, then I'll continue polishing the implementation if the RFC changes or gets accepted ^^ |
I wish a better suggestive / expressive keyword was chosen, e.g. "Any" like in TypeScript. Mixed sounds like mixing the things, like union types. So IMHO it will lead the devs being confused such: "what the type was / will mixed with". |
@K-gun Yes, |
Thanks for your work on reviving this! Hope we can get it for 8.0 :D |
It's a surprise for me to find this PR. I am not very happy to see my work stolen instead of reaching out to me (Twitter or even an e-mail) first. Especially when done by a PHP member directly (who could even push directly to my PR). I actually dropped @nikic an e-mail last month to see how to move some RFCs (including mixed) forward for PHP 8 but didn't really want to insist on it and ping him here or on Twitter directly. |
@Majkl578 Please have a look again at your PR where I asked about your plans with the Also, your work is not stolen, since union types required a completely different implementation. |
I am not very active on GitHub or in OSS in general in the past year.
I am not aware of any deadlines for responses being set. You could've contacted me on Twitter (my DMs are open) and we could've talked about the RFC itself and how to improve it and move towards voting. But you did not. So I expect you'll now take over my RFC as well? (If I was not interested in finishing the RFC the PR would be already closed and RFC withdrawn/inactive.) |
It's not because you change the lyrics of a song that it's not a steal when you use it. Support for mixed type would be great. I can't wait to use it ! |
@kocsismate; I suppose, historical reasons cannot be an argument to go with them. If this were true, the world would remain as it was in the history. This is same for cultural perspective. Eg. "real" type removed but it was in PHP culture for years, and I can bring many examples here like that. And also we all know that the official documentations are untouched for decades, unless any comment made like that ie: "with version 7.4 bla bla" (yes it is true, my comment is removed from strip_tags document and added 7.4 note a few weeks ago). And withal, I can say that; in many open source library, I have seen billion and billion of PHPDoc's that use any keyword during my 15 years of PHP experience so far. So, here is my humble recommendation; please, allow people to use both Meanwhile, seems All beside I have a question with the source below; is it forbidden writing something like this Source: if ((type_mask == MAY_BE_ANY) && allow_null) {
zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be nullable");
} Source link: https://github.com/php/php-src/pull/5313/files#diff-3a8139128d4026ce0cb0c86beba4e6b9R5671 A simple use case for nullables: function find_post(?any $max_id = null): ?array {
if (is_null($max_id))
return db_find("select * from `posts` order by `id` desc limit 1");
if (!is_numeric($max_id)) // more strict control maybe
return null;
if (is_string($max_id))
$max_id = intval($max_id);
return db_find("select * from `posts` where `id` < {$max_id} order by `id` desc limit 1");
} |
Hello @K-gun,
Best regards, |
Thanks @moliata for the eloquent answer! I agree with all your points. :) |
@moliata; even you were not the addressee of my comments and questions, thanks anyway for your reply.
PS: This reply is not supposed to be harsh, so please do not take it as such. -- PHP is called "The Orc Language" for years because of its oddnesses, adding more odd and exotic things will make it remain as it called. So, do I love PHP? Yes, more than all other languages even while thinking about switching to another language nowadays after that much time. But at the same time, I know the end the of the movie, Orcs lose... |
5da1150
to
825b6a5
Compare
Travis CI build succeed, but the statuses weren't port back to GitHub: https://travis-ci.org/github/php/php-src/builds/689173929. |
Thanks for this RFC @kocsismate @Danack, I'll now be able to work on mine at #4177 💛 Shall we also close #2603? I can't as I'm blocked by its author. |
One thing I didn't see after a quick look at this PR or the RFC: Should Generators be allowed to have an explicit return type of mixed? I'd have assumed they should, because it's returning a value and omitting the return type is allowed After building php 8 with this change:
also, with |
@TysonAndre Nice catch! Mixed should indeed be permitted for generators. |
Fixes an edge case overlooked in php#5313
Fixes an edge case overlooked in php#5313 Generators are objects, so `object` should have also been allowed in addition to `iterable` and `Traversable`
That is sad. Why not properly saying For me
if it can be nullable and
Where you don't need an This creates an unnecessary amount of additional checks where they could be avoided. |
As of PHP 8.0, `mixed` can be used as a parameter type for function declarations. The RFC explicitly does not allow for the `mixed` type to be combined with the nullability indicator. While in itself, that is not a cross-version compatibility issue, I have made a very conscious choice to add a check for this anyway as - while discouraged with `mixed` being a soft reserved keyword -, prior to PHP 8, a class _could_ be named `mixed`, so a `?Mixed` type hint referring to such a class could exist in code and would be a cross-version compatibility issue as since PHP 8 that will throw a "Fatal error: Mixed types cannot be nullable". Refs: * https://wiki.php.net/rfc/mixed_type_v2 * php/php-src#5313 * php/php-src@aec4c0f Includes unit tests.
As of PHP 8.0, `mixed` can be used as a return type for function declarations. The RFC explicitly does not allow for the `mixed` type to be combined with the nullability indicator. While in itself, that is not a cross-version compatibility issue, I have made a very conscious choice to add a check for this anyway as - while discouraged with `mixed` being a soft reserved keyword -, prior to PHP 8, a class _could_ be named `mixed`, so a `?Mixed` type hint referring to such a class could exist in code and would be a cross-version compatibility issue as since PHP 8 that will throw a "Fatal error: Mixed types cannot be nullable". Refs: * https://wiki.php.net/rfc/mixed_type_v2 * php/php-src#5313 * php/php-src@aec4c0f Includes unit tests. Includes removing the `testNoViolationsInFileOnValidVersion()` test as the sniff will now throw errors either way.
As of PHP 8.0, `mixed` can be used as a type for property declarations. The RFC explicitly does not allow for the `mixed` type to be combined with the nullability indicator. While in itself, that is not a cross-version compatibility issue, I have made a very conscious choice to add a check for this anyway as - while discouraged with `mixed` being a soft reserved keyword -, prior to PHP 8, a class _could_ be named `mixed`, so a `?Mixed` type hint referring to such a class could exist in code and would be a cross-version compatibility issue as since PHP 8 that will throw a "Fatal error: Mixed types cannot be nullable". Refs: * https://wiki.php.net/rfc/mixed_type_v2 * php/php-src#5313 * php/php-src@aec4c0f Includes unit tests.
@kocsismate Is it allowed to use
We need this in order to have IDE autocomplete for |
@DriverCat No, it isn't allowed in a type declaration. You can freely do so though in PHPDoc, but I'm not sure whether static analysers complain about it. However, unless your method returns other types besides the two classes, you might be able to get rid of mixed:
|
Thanks for the answer! Thats exactly the problem that it allows custom classes return. |
Revival of #2603
RFC: https://wiki.php.net/rfc/mixed_type_v2