Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:71232 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 81849 invoked from network); 18 Jan 2014 03:52:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Jan 2014 03:52:37 -0000 Authentication-Results: pb1.pair.com smtp.mail=julienpauli@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=julienpauli@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.49 as permitted sender) X-PHP-List-Original-Sender: julienpauli@gmail.com X-Host-Fingerprint: 209.85.219.49 mail-oa0-f49.google.com Received: from [209.85.219.49] ([209.85.219.49:54777] helo=mail-oa0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5E/E0-10526-08AF9D25 for ; Fri, 17 Jan 2014 22:52:34 -0500 Received: by mail-oa0-f49.google.com with SMTP id i7so3251685oag.22 for ; Fri, 17 Jan 2014 19:52:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; bh=cmKRAkFp7h4dNtZs+EC476WDHB/fydtFNBx5+uJ6xPU=; b=SaV9PyHqgM2xdldsuaT49FVHTx/cpVyogskoQmFs7Twh5x5DnuSAk8RINLbSOeG/TR e2qozd+C8j/OqqWfyVIdkJaC4mX03kkrj8+Dix7zKxey26+lZ1hSYtreCl9YLn01iLfi 2XIzBUqR1qKJW6tqArW0BLwJpH6lDPKiTrj6q2wCw4aERrt1oIE6zOUXBDLLhp+QE0Du x14+WDRaARSD9TXNWFlGP5R936IL0sjPUrd102rHbxHmzcMCyHsOqfYC8UXBlkupAhYZ bG3mbFCtr9F5CtT7OZUnIeT/cFk6ve7o0XAud8F7anLp4Cee26euLstbO4WeF0w34CGM wMtQ== X-Received: by 10.60.246.104 with SMTP id xv8mr4855021oec.18.1390017149782; Fri, 17 Jan 2014 19:52:29 -0800 (PST) MIME-Version: 1.0 Sender: julienpauli@gmail.com Received: by 10.182.235.46 with HTTP; Fri, 17 Jan 2014 19:51:49 -0800 (PST) In-Reply-To: <002001cf12da$2bfbda90$83f38fb0$@tutteli.ch> References: <001301cf1227$6d082ab0$47188010$@tutteli.ch> <002d01cf1234$01e6dc60$05b49520$@tutteli.ch> <004c01cf123d$35730870$a0591950$@tutteli.ch> <52D71748.1090402@googlemail.com> <52D71FAE.8030002@ajf.me> <005001cf124f$3a40df00$aec29d00$@tutteli.ch> <20140116110127.202079vzjsj76n7b@webmail.tutteli.ch> <0B.B1.24763.139B7D25@pb1.pair.com> <002001cf12da$2bfbda90$83f38fb0$@tutteli.ch> Date: Sat, 18 Jan 2014 04:51:49 +0100 X-Google-Sender-Auth: U3qtdrYrOICiEAGcJzm2qCgSk1k Message-ID: To: Robert Stoll Cc: Matthieu Napoli , PHP Internals Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Introducing "Array Of" RFC From: jpauli@php.net (Julien Pauli) On Thu, Jan 16, 2014 at 5:44 PM, Robert Stoll wrote: >> How is type checking a bad design? >> >> Between: >> >> function (array $foos) { >> foreach ($foos as $foo) { >> if (! $foo instanceof Foo) { >> throw new InvalidArgumentException("..."); >> } >> $foo->bar(); >> } >> } >> >> and: >> >> function (array $foos) { >> foreach ($foos as $foo) { >> $foo->bar(); // BOOM, fatal error if not correct object given >> } >> } >> >> which one is worse? > > You got me wrong, I don't think that type checking as such is bad but typ= e checking in the way the RFC promotes it is bad design IMO. If I expect an= array with members of a certain type (regardless if including null or not)= then I would not write any of the above code because this is exactly the b= ad design I was writing about. > Wouldn't you agree with me that it would be better to type check the entr= ies when they are added rather than during each function call? > Imagine function foo(Foo[] $foos){} which calls bar and baz which in turn= call several other functions which all have the same type hint Foo[]. > The result would be that you have increased your runtime complexity by th= e factor of function calls. That's really ugly and I don't think that the l= anguage itself should promote such bad design. If you go further and suppor= t multi-dimensional arrays the same way then you would add a complexity of = n*m for each function call with such a type hint. I'm absolutely +1 with such a statement. > > However, I like the basic idea of the RFC, namely typed arrays. I think a= better implementation would be to introduce that an array holds (visible o= nly internally - could be exposed to userland later) the defined type and p= erforms the necessary checks during addition and therefore the runtime comp= lexity for the type hint checks would no longer be linear but constant. Yet= , it is probably better to introduce another internal data structure for th= is purpose in order that the type hint "array" does not suffer from the add= itional checks. > An alternative approach, without changing the language, would be to provi= de an additional SPL data structure instead which performs the type check d= uring the addition of an entry. Yes, yes and yes again. +1 This RFC shows that we lack "array of" as a whole structure in PHP, like Java has Vectors. We already talked about this in the past. I'm in favor of designing a new "typed array" structure , instead of adding a new syntax that checks things at runtime of every function call, mainly by iterating over the array at runtime. I dont think we can accept such a penalty, particulary in the case of nested function calls with the same signature, all requiring the engine to iterate for type checking. Julien.P