On 4/18/2016 7:22 AM, Marcio Almada wrote: > 2016-04-17 23:56 GMT-04:00 Larry Garfield <la...@garfieldtech.com>: >> Rather than debate the relative merits of Null as a concept, since I think >> all agree removing Null from the language is not even a thing, what do you >> think of my recommendation to NOT have the ? shorthand for "or null" on >> types, but keep the |null for union types? That is, if you want to return >> Foo or null, you have to use the long-hand version: >> >> function foo($a) : Foo|null >> >> That >> >> 1) Allows for explicit or-null behavior. >> 2) Doesn't make it quite so easy, which hopefully discourages (but does >> not prohibit) its usage. >> 3) Neatly avoids the ?Foo / Foo? question entirely. >> >> I think that's a win-win arrangement. >> > I can agree with that. > > Language design wise, if you have no way to forecast how language features > independently proposed are going to interact then it's a good idea to have > a converging strategy. Fortunately, this seems to be happening almost > spontaneously towards union types representation, as in the "multiple > catch" RFC (https://wiki.php.net/rfc/multiple-catch) now in voting phase. > > Even if we decide not to commit to a full union types implementation now, > the design process will be much easier in the future if we decide to use > Foo|null instead of Foo? or ?Foo and we will not end up with 2 or 3 ways > alternative to declare the same thing. >
I actually do not see a problem with the shorthand notation. I am usually an extreme opponent of duplicated or aliased functionality because it increases the cognitive load to keep all this crap in your mind. This is however a useful shortcut and shortcuts are good things in terms of usability if done write. Think of <Ctrl> + <c> vs. right click > copy (or any other example of useful keyboard shortcuts). The usage of *null* as an union type is the most common case compared to all the others that are truly rare in comparison. There are other shortcuts in PHP that are perfect: - Iterator vs. Generator: they can achieve the exact same thing but slightly different and a generator can almost always be used to replace an iterator with less code, even if the special effects of it are irrelevant. - `$x = $x + $y` vs. `$x += $y`: we have many of those and that is just fine as it is. - `$x = $x + 1` vs. `++$x` vs. `$x++`: ditto - `<?php echo 'Hello World'` vs. `<?= 'Hello World'`: ditto - if-else vs. `?:`: ditto - public function vs. function in interfaces: I think the sole reason why people include the public access modifier is because no PHP IDE automatically adds the public while implementing the methods. - while vs. do-while vs. for vs. foreach: It is possible to achieve everything with a while loop. - ... Plain and simple, shorthand functionality is not always and with no exceptions bad. On the contrary, it can make your life a whole lot easier (I think this is best illustrated by the three loop constructs which are all super usable). That being said, I can also live with leaving out the shorthand notation. Simply because we currently have to write it out fully in PhpDoc anyways (not that this ever discouraged anyone to misuse *null* everywhere, even if they properly documented everything). Last but not least, 3) is already answered for me. It must be prefix to allow later extensions like generics. Plus HHVM already has it as a prefix which is another good argument in my book. -- Richard "Fleshgrinder" Fussenegger
signature.asc
Description: OpenPGP digital signature