Re: [RFC][Vote] Return Types

From: Date: Thu, 06 Nov 2014 07:43:16 +0000
Subject: Re: [RFC][Vote] Return Types
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-78789@lists.php.net to get a copy of this message
It's clear, that covariant types are more smart, but not supporting them, won't prevent access to more specific type properties and methods. We are not C++ or Java, and we don't need to cast objects to more specific types. On the other hand covariance requires all return types to be defined before class binding. It may be a serious new problem. For example you won't be able to compile the following code at all? <?php class A { function foo(): C {} } class B extends A { function foo(): C {} } class C extends B { function foo(): C {} } ?> The similar code with argument type hinting works fine. It's just a first example I could imagine, I believe, we will get more... Thanks. Dmitry. On Thu, Nov 6, 2014 at 5:01 AM, Levi Morrison <morrison.levi@gmail.com> wrote: > To demonstrate the value of covariance and why static alone is > not > sufficient, here is a small example: > > interface Enumerable extends \IteratorAggregate { > function getIterator(): Enumerator; > } > > class Vector implements Enumerable, \ArrayAccess, \Countable { > function getIterator(): VectorEnumerator { /* … */ } > } > > class VectorEnumerator implements Enumerator, \Countable { > /* … */ > } > > This shows why covariance is important for two reasons: > First, it shows that static isn't sufficient. The VectorEnumerator is > not the calling class, so self and static are not applicable. > Second, without covariance you could only declare a return type of > Enumerator for Vector::getIterator(); a calling class couldn't rely on > the properties of a VectorEnumerator that are unique to it, such as > \Countable. >

« previous php.internals (#78789) next »