Re: Introducing "Array Of" RFC

From: Date: Thu, 16 Jan 2014 10:38:35 +0000
Subject: Re: Introducing "Array Of" RFC
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Hi,


Am Mittwoch, 15. Januar 2014 um 23:00 schrieb Robert Stoll:

> Fair enough, the example with the English sentence makes pretty much sense from an average
> user's point of view and thus
> most PHP users would probably agree with you (don't take it wrong, I don't imply you
> are average or something similar). 
> However, this should not be a decision based on whether many people are preferring it this way
> or the other way round in
> my opinion, since the restriction is going to make it impossible to use it for the use cases
> where NULL is a valid
> entry. 
> So, we should ask ourselves, do we really want to exclude a whole bunch of valid use cases or
> should we decline the
> restriction and put more effort in providing alternatives for non-null -containing collections.
> 
> See, if we do not introduce the restriction and provide a non-null-containing collection then
> all use cases can be
> supported. That is the way to go IMO.
> Or do you have another idea how one can specify that the array can contain null?
> And please consider, if you should suggest "function foo(Foo[] $foo=null)" implies
> that $foo can also contain NULL then
> we have another vague concept in PHP which has a double meaning -> $foo can be NULL and
> entries in $foo can be NULL. But
> maybe one does not want to allow NULL for $foo but only for members of $foo.
> 
> 



if you pass an array which mixes both objects and null and you want to work on the objects, you
still have to do the checks yourself:

function f (A[] $list)
{
    foreach ($list as $entry)
    {
        if (!is_null($entry))
        {
            $entry->doSth();
        }
    }
}


But in this case you could just omit the type hint and check it internally:

function f ($list)
{
    foreach ($list as $entry)
    {
        if (!is_null($entry) && ($entry instanceof A))
        {
            $entry->doSth();
        }
    }
}



The thought of this proposal is that the complete checking is done with just the type hint.
And allowing null would make this obsolete.

Also, you would then have the duplicated runtime impact:
you need to check the array twice:

1. check for null & A (type hint)
2. check for A

instead of just one. 



Cheers
Jannik 



Thread (46 messages)

« previous php.internals (#71174) next »