Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:71189 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37148 invoked from network); 16 Jan 2014 16:44:12 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jan 2014 16:44:12 -0000 Authentication-Results: pb1.pair.com header.from=php@tutteli.ch; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=php@tutteli.ch; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain tutteli.ch designates 80.74.154.78 as permitted sender) X-PHP-List-Original-Sender: php@tutteli.ch X-Host-Fingerprint: 80.74.154.78 ns73.kreativmedia.ch Linux 2.6 Received: from [80.74.154.78] ([80.74.154.78:47856] helo=hyperion.kreativmedia.ch) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 03/C2-21406-B5C08D25 for ; Thu, 16 Jan 2014 11:44:12 -0500 Received: (qmail 6993 invoked from network); 16 Jan 2014 17:44:08 +0100 Received: from heim-032-99.raab-heim.uni-linz.ac.at (HELO RoLaptop) (193.171.32.99) by ns73.kreativmedia.ch with (AES128-SHA encrypted) SMTP; 16 Jan 2014 17:44:08 +0100 To: "'Matthieu Napoli'" , 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> In-Reply-To: <0B.B1.24763.139B7D25@pb1.pair.com> Date: Thu, 16 Jan 2014 17:44:06 +0100 Message-ID: <002001cf12da$2bfbda90$83f38fb0$@tutteli.ch> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQGahXeLh/KZbd8XAeJ9KP0Xitay+AEcU2WhAaqmqGABFinRgAGUH60GAsS5wSoDHy62oAKq0EPNApG0KWABZZ/6VwJfgrHWAcL4bIgCA1yDhgETP0JZmicYYfA= Content-Language: de-ch Subject: RE: [PHP-DEV] Introducing "Array Of" RFC From: php@tutteli.ch ("Robert Stoll") > How is type checking a bad design? >=20 > Between: >=20 > function (array $foos) { > foreach ($foos as $foo) { > if (! $foo instanceof Foo) { > throw new InvalidArgumentException("..."); > } > $foo->bar(); > } > } >=20 > and: >=20 > function (array $foos) { > foreach ($foos as $foo) { > $foo->bar(); // BOOM, fatal error if not correct object given > } > } >=20 > which one is worse? You got me wrong, I don't think that type checking as such is bad but = type 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 bad design I was writing about. Wouldn't you agree with me that it would be better to type check the = entries 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 = the factor of function calls. That's really ugly and I don't think that = the language itself should promote such bad design. If you go further = and support multi-dimensional arrays the same way then you would add a = complexity of n*m for each function call with such a type hint.=20 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 only internally - could be exposed to userland later) the = defined type and performs the necessary checks during addition and = therefore the runtime complexity for the type hint checks would no = longer be linear but constant. Yet, it is probably better to introduce = another internal data structure for this purpose in order that the type = hint "array" does not suffer from the additional checks. An alternative approach, without changing the language, would be to = provide an additional SPL data structure instead which performs the type = check during the addition of an entry.=20 Nevertheless, the implementation of the RFC as such is not useless. We = could introduce a function is_arrayOf(array $arr, $type, = $nullable=3Dfalse) which could perform the check as implemented in the = pull request (including the nullable option). And I suppose you can come = up with valid uses cases where such a check makes actually sense and = worth the new function.