Re: Re: Improving PHP's type system

From: Date: Fri, 15 Apr 2016 16:57:09 +0000
Subject: Re: Re: Improving PHP's type system
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14  Groups: php.internals 
Request: Send a blank email to internals+get-92341@lists.php.net to get a copy of this message
On 14.04.2016 at 21:32, Jordi Boggiano wrote: > I don't really think it's much more complex to grasp 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). There may not be very many cases for union types, but that might not hinder some programmers to make extensive use of the feature, which others may encounter in their APIs. > 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. It occurs to me that this is an instance of parameter overloading: accept one of two (or more) unrelated types and handle them separately in a single function. An alternative would be to have two (or more) (factory) functions, each accepting one of the alternative types (perhaps delegating to a common function for the common code, if necessary). -- Christoph M. Becker

« previous php.internals (#92341) next »