Re: Allow dropping typehints during inheritance
| From: | Levi Morrison | Date: | Thu, 05 Feb 2015 21:52:06 +0000 |
| Subject: | Re: Allow dropping typehints during inheritance | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-81966@lists.php.net to get a copy of this message | ||
On Thu, Feb 5, 2015 at 5:14 AM, Andrea Faulds <ajf@ajf.me> wrote:
> Hi Julien,
>
>> On 5 Feb 2015, at 12:10, Julien Pauli <jpauli@php.net> wrote:
>>
>> If we allow larger type, why doesn't such code work ?
>>
>> interface A { }
>> interface B extends A { }
>>
>> class C {
>> public function foo(A $a) { }
>> }
>>
>> class D extends C {
>> public function foo(B $a) { } // E_STRICT
>> }
>>
>> This is wrong IMO.
>
> Well, firstly that’s the wrong way round: inheriting classes can only *increase* the range of
> supported values, but you’ve done exactly the opposite.
>
> But even if you fixed your code, you’d still have an error. This ispresumably because doing
> anything other than simple invariance causes enormous problems related to compilation order and
> autoloading, as we discovered with the Return Types RFC.
I would hardly call them "enormous problems". Just normal problems
that I think were easier to avoid than to deal with given my schedule
and the time-frame for PHP 7.
To chime in regarding allowing contravariant parameter types: I
struggle to find use cases for it. I can only think of one use-case
and it's flawed: when something has declared an Iterator parameter and
you widen it to include Traversable. However, even though Traversable
is a parent of Iterator you can't directly call iterator methods on
it, so it could break the calling code despite the super-type check
passing.