Re: [RFC] Return Type Declarations pre-vote follow-up
On 09.05.2014 22:06, Josh Watzman wrote:
On May 9, 2014, at 8:09 AM, Levi Morrison <
[email protected]> wrote:
First minor issue: you have a return type "self". This is an LSB type, effectively the type of "new static". Levi, I remember discussing this with you in IRC, but not the result of that discussion. What is the reason for "self"? The Hack type system calls it "this", which I admit is a little confusing since it is the type of more things than just the literal variable "$this" ("new static" for example is also of type "this"). But the type "static" is much more consistent with the existing LSB language, i.e., "new static".. So is there any reason to call it "self" over "static" or "this"?
The keyword "self" should be a valid return type and simply alias the current class.
The keyword "static" should be a valid return type and simply alias last class of the list of inheritance.
Example 1: (fluent interface)
class Fluent {
protected $property;
public function setProperty($val) : static {
$this->property = $val;
return $this;
}
}
Example 2: (extendable singleton)
class Singleton {
protected static $inst;
public static function getInstance() : static {
static::$inst = static::$inst ?: new static();
return static::$inst;
}
}
"this" isn't a keyword and from reading perspective it is pointing to a variable $this but $this is a object instance and not a type. The type of the $this object is the same class "static" is pointing to as shown in the fluent interface example.
Thanks for putting this together -- the FB team is really looking forward to it.
Josh Watzman
Marc
Thread (15 messages)