RE: [PHP-DEV] Introducing "Array Of" RFC
| From: | Robert Stoll | Date: | Fri, 28 Feb 2014 14:25:49 +0000 |
| Subject: | RE: [PHP-DEV] Introducing "Array Of" RFC | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-72862@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Philip Sturgeon [mailto:pjsturgeon@gmail.com]
> Sent: Friday, February 28, 2014 2:54 PM
> To: PHP Developers Mailing List
> Subject: Re: [PHP-DEV] Introducing "Array Of" RFC
>
> 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.
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
I brought up that null in an array is a valid use case at the beginning of this discussion and that
the RFC should deal
with such use cases - otherwise it is incomplete. I still claim it is incomplete if this topic is
not addressed but I am
happy if option 2 is implemented later on.