Re: Introducing "Array Of" RFC

From: Date: Thu, 16 Jan 2014 13:06:38 +0000
Subject: Re: Introducing "Array Of" RFC
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
> https://wiki.php.net/rfc/arrayof
>
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


Thread (46 messages)

« previous php.internals (#71184) next »