Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:71367 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 60235 invoked from network); 20 Jan 2014 22:33:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Jan 2014 22:33:29 -0000 Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 198.187.29.247 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 198.187.29.247 imap4-3.ox.registrar-servers.com Received: from [198.187.29.247] ([198.187.29.247:51800] helo=imap4-3.ox.registrar-servers.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 26/7F-02192-834ADD25 for ; Mon, 20 Jan 2014 17:33:29 -0500 Received: from localhost (localhost [127.0.0.1]) by oxmail.registrar-servers.com (Postfix) with ESMTP id 2C09A56007F; Mon, 20 Jan 2014 17:33:25 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at imap4.ox.registrar-servers.com Received: from oxmail.registrar-servers.com ([127.0.0.1]) by localhost (imap4.ox.registrar-servers.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id tUf64HRriZ44; Mon, 20 Jan 2014 17:33:25 -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 B25F656007B; Mon, 20 Jan 2014 17:33:23 -0500 (EST) Message-ID: <52DDA431.2080600@ajf.me> Date: Mon, 20 Jan 2014 22:33:21 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: Stas Malyshev , Andrey Andreev , "internals@lists.php.net" References: <52D865C7.4070009@sugarcrm.com> <52DD9DC4.6060502@sugarcrm.com> In-Reply-To: <52DD9DC4.6060502@sugarcrm.com> 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 20/01/14 22:05, Stas Malyshev wrote: > Hi! > >> Please excuse my lack of knowledge, but if I understand this correctly, >> this is how generics would work ... using arrays, ignore the syntax and >> details such as child classes, interfaces, etc.: >> >> $fruits = Array; - declare that $fruits is an array that can only >> contain instances of class Fruit >> function eat(Array $apples) - function eat() will only accept >> arrays that are declared in the same way as $fruits above > > There's two ways to understand "will only accept": > > 1. There's a class/type which I call ArrayOfFruit for simplicity. eat() > checks if $apples is instance of ArrayOfFruit. Some magic inside > ArrayOfFruit ensures ArrayOfFruit always contains only Fruit. > 2. eat() goes over $apples which is a regular array, and checks if every > element in it is instanceof Fruit. > > First way is essentially templates, second is not. So far I've seen most > advocacy in this RFC to go towards the second way. > Myself, I want 2 now with syntax that will eventually work for 1 too in the future. Soft generics. Array could implicitly cast to Array so long as all its members are fruit, in future. But for now, it will only be 2. This adds an incentive to add generics: better performance by not requiring a runtime check when an Array is passed an not an Array of Fruit. -- Andrea Faulds http://ajf.me/