Re: Introducing "Array Of" RFC

From: Date: Fri, 28 Feb 2014 13:53:50 +0000
Subject: Re: Introducing "Array Of" RFC
References: 1 2 3 4 5 6 7 8 9 10 11  Groups: php.internals 
Request: Send a blank email to internals+get-72861@lists.php.net to get a copy of this message
On Thu, Feb 27, 2014 at 4:34 PM, Marco Pivetta <ocramius@gmail.com> wrote: > > > On 27 February 2014 22:11, Philip Sturgeon <pjsturgeon@gmail.com> wrote: >> >> So, should we allow nulls in an arrayof-style type hint? >> > > null has its own type, which is mismatching the type required > by the > array-of syntax when requesting, for example, Foo[]. > > If null was allowed in the array, as a user, I'd still be > forced to > array_filter() on every call, so it would defeat the new > feature > completely. > > Cheers, > > Marco Pivetta > > http://twitter.com/Ocramius > > http://ocramius.github.com/ > I completely agree with you and the other folks saying: "Allowing nulls would defeat the new feature completely". Personally I would not see myself using the "Foo or null", and if I wanted to in the future I could very easily just not type hint the array. On Thu, Feb 27, 2014 at 4:34 PM, Andrea Faulds <ajf@ajf.me> wrote: > > On 27 Feb 2014, at 21:11, Philip Sturgeon <pjsturgeon@gmail.com> wrote: > > So, should we allow nulls in an arrayof-style type hint? > > > I previously suggested using the C#-style ? to specify nullability. Why > can't we do that? > > -- > Andrea Faulds > http://ajf.me/ We could potentially add in the ? syntax later on, if people seemed to want the feature enough? Just to make sure we are on the same page here, nullable arrays are fine. Arrays with nulls inside are not. Both of these are acceptable: function foo(Bar[] $baz) // accepts [new Bar, new Bar] function foo(Bar[] $baz = null) // accepts [new Bar, new Bar] or null Some people are suggesting the following alternatives. 1. Let nulls in there regardless of default var hinting: function foo(Bar[] $baz) // accepts [new Bar, null, new Bar, null] function foo(Bar[] $baz = null) // accepts [new Bar, null, new Bar, null] or null This is what many describe as a completely useless version of the feature, as array_filter would need to be run to ensure you're only working with objects of a certain type (which is 90% of the use-case for this RFC). Or, 2. Offer optional syntax for arrays containing nulls function foo(Bar[]? $baz) // accepts [new Bar, null, new Bar, null] function foo(Bar[]? $baz = null) // accepts [new Bar, null, new Bar, null] or null Pro: Keeps nulls out of the array by default, offers a way to say "Bar or null" Con: Potentially syntax soup.

« previous php.internals (#72861) next »