Re: [RFC][Vote] Return Types

From: Date: Thu, 06 Nov 2014 02:01:12 +0000
Subject: Re: [RFC][Vote] Return Types
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-78769@lists.php.net to get a copy of this message
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 (#78769) next »