Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:71273 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37873 invoked from network); 19 Jan 2014 17:59:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Jan 2014 17:59:02 -0000 Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 198.187.29.241 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 198.187.29.241 imap3-1.ox.registrar-servers.com Received: from [198.187.29.241] ([198.187.29.241:32846] helo=imap3-1.ox.registrar-servers.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 48/0C-61840-2621CD25 for ; Sun, 19 Jan 2014 12:58:59 -0500 Received: from localhost (localhost [127.0.0.1]) by oxmail.registrar-servers.com (Postfix) with ESMTP id C640E2A006A; Sun, 19 Jan 2014 12:58:55 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at imap3.ox.registrar-servers.com Received: from oxmail.registrar-servers.com ([127.0.0.1]) by localhost (imap3.ox.registrar-servers.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id utIr91cpvPEt; Sun, 19 Jan 2014 12:58:55 -0500 (EST) Received: from [192.168.0.200] (unknown [176.25.177.94]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by oxmail.registrar-servers.com (Postfix) with ESMTPSA id 825232A0053; Sun, 19 Jan 2014 12:58:54 -0500 (EST) Message-ID: <52DC125C.4010609@ajf.me> Date: Sun, 19 Jan 2014 17:58:52 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: Sara Golemon , Philip Sturgeon CC: "internals@lists.php.net" References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Introducing "Array Of" RFC From: ajf@ajf.me (Andrea Faulds) On 16/01/14 13:06, Sara Golemon wrote: >> 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 { > protected T $foo; > > public function getFoo(): T { > return $this->foo; > } > > pubilc function setFoo(T $foo) { > $this->foo = $foo; > } > } > > $f = new Foo(); > $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 or array > > function foo(array $numIdxArrayOfBar, array > $numIdxArrayOfBaz, array $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> 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/array 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" 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/