Re: Introducing "Array Of" RFC

From: Date: Sun, 19 Jan 2014 17:58:52 +0000
Subject: Re: Introducing "Array Of" RFC
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message


On 16/01/14 13:06, Sara Golemon wrote:
I'm going to fowl up this conversation by describing what HHVM's Hack syntax does for this. Partly to encourage PHP to do the same, partly to just lay out the edge cases we thought about on the way to developing the syntax we landed with. * Generics Syntax - Allows creation of typed classes which repeat common functionality with specific implementations class Foo<T> { protected T $foo; public function getFoo(): T {
     return $this->foo;
} pubilc function setFoo(T $foo) {
     $this->foo = $foo;
} } $f = new Foo<Bar>(); $f->setFoo(new Bar()); $b = $f->getFoo(); * Typed arrays - Arrays are a special primitive which can be typed as implicitly or explicitly numerically indexed, or associative, and to specific types: array<Tv> or array<Tk,Tv> function foo(array<Bar> $numIdxArrayOfBar, array<int,Baz> $numIdxArrayOfBaz, array<string,$Bong> $assocArrayOfBong) { ... } Since the value type is unconstrained, it can be a generic type as well, so a numerically indexed array of associative arrays of strings would look like array<array<string,string>> and so on. * Nullable types - Any type preceeded by a question mark may be that type or null: function foo(?Bar $barOrNull, Baz $bar, ?Blong $blongOrNull) {...} * Soft types - Any type preceeded by an at sign is hinted as that type, but not checked: function foo(@Bar $IHopeYoureABarObject) {...} ---------------------------------------------- Within the scope of this RFC, I want to encourage array<Tv>/array<Tk,Tv> syntax (with nullable/soft modifiers) as I think they cover every edge case presented by this discussion so far. It also covers elements not handled by the proposed syntax (index types). The syntax will be familiar to developers coming from C++ and it leaves room to entertain generics in general. -Sara
After a little thought, I think I'd rather we use the HHVM syntax here. Having "Array" for an untyped array but "Bar[]" for a typed one is silly. "Array" and "Array<Bar>" make more sense. Being able to type keys would be nice, too. However, I have but one little issue with the HHVM syntax. Why is the ? before the type name? I much prefer the C#-style Bar? to this confusing ?Bar. I suppose this is only a minor issue, though. -- Andrea Faulds http://ajf.me/

Thread (46 messages)

« previous php.internals (#71273) next »