Re: Re: Improving PHP's type system
| From: | Jordi Boggiano | Date: | Thu, 14 Apr 2016 19:32:21 +0000 |
| Subject: | Re: Re: Improving PHP's type system | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-92313@lists.php.net to get a copy of this message | ||
Heya,
On 14/04/2016 19:59, Stanislav Malyshev wrote:
I don't really think it's much more complex to graspI don't know what is complicated about "string|Stringable" or "Foo|Bar" since it is super self-explanatory. However, I find myself checking theIt may be self-explanatory for you. It's much less self-explanatory for somebody just starting to learn. It is also very dangerous - if it's either Foo or Bar, can you call Foo::stuff on it or not? If it's string or not string, can you call strlen on it? Etc., etc. It adds a lot of cognitive load and complicates the whole picture.
Foo|Bar $foo than only Foo $foo. I mean once you grasp the concept of type hints you probably have by then a good understanding of || and hopefully can derive understanding from there.
That said I agree it's rarely useful, and as such I am not expecting we'll see this all over the place, it's just nice to have when you need it, but I can't think of very many valid cases (nullable types are much more common).
Please take that into consideration as well when arguing that it adds complexity. If it's rarely seen in the wild it's not very valuable but it's *also* not hindering newcomers often.
You may have a specific use case where it is useful (which we have yet to see btw) but please remember it's a language with literally millions of use cases and users.Just to highlight one use case I can think of, here is one: https://github.com/Seldaek/monolog/blob/master/src/Monolog/Handler/MongoDBHandler.php#L51-L53 We need some sort of Mongo thing, and there are two available with different interfaces, so here both are accepted but as you see in the code below they are handled kinda separately. It would just save us those 3 lines of check if we could type-hint appropriately. And it would save us one line of phpdoc as well because IDEs could infer the information from the code. Cheers -- Jordi Boggiano @seldaek - http://seld.be