Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101033 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 20187 invoked from network); 2 Nov 2017 19:21:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Nov 2017 19:21:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.28 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.28 out4-smtp.messagingengine.com Received: from [66.111.4.28] ([66.111.4.28:60815] helo=out4-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DA/05-09857-9207BF95 for ; Thu, 02 Nov 2017 14:21:14 -0500 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 846A720BE6 for ; Thu, 2 Nov 2017 15:21:10 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute7.internal (MEProxy); Thu, 02 Nov 2017 15:21:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=43SDpm cS9EaXpKtjg2Fut38dV8iHlcqJqEY5NNdCWck=; b=YQtF7a+MQ1q3DPd9I00lXf 1Ca00m5lXmGjKvn3hBJb8AsJBxZkDyET33nkoUsrnNiQW7a+LQI1eM3LGrK+Z+3a v0CWfLBsEYMCFtXzZn+xBy6GMxIkRr24ojw0vEvIjEP/x0mvPJMDueuG0jLrvhtO EgW9IkZ6ME3555GeHmOVzmWXZZ+ooubfbV1zvk3L7Vv8vbwd856JEozxQgBBuFsq E2xyToPbt9xIa+WaeMZ0nlkNkjS6U8oSMSnhuolDTE8sojB1mnZlYcMEwRuuqA20 kFq3UXaPKmS8G9L0BeHm1wc0VsRkF+mw+jwFdaLYRrZ2kc4gkTfKAhxviEN7sdWA == X-ME-Sender: Received: from [192.168.42.5] (216-80-30-152.s3222.c3-0.frg-ubr1.chi-frg.il.cable.rcncustomer.com [216.80.30.152]) by mail.messagingengine.com (Postfix) with ESMTPA id 486A5241D9 for ; Thu, 2 Nov 2017 15:21:10 -0400 (EDT) To: internals@lists.php.net References: <2C3A2443-8680-4FA7-BD25-872472506D03@gmail.com> Message-ID: <2ddc5a35-6117-2507-2759-71f7996797c8@garfieldtech.com> Date: Thu, 2 Nov 2017 14:21:09 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Language: en-US Subject: Re: [PHP-DEV] RFC - Array Of for PHP 7 From: larry@garfieldtech.com (Larry Garfield) On 11/01/2017 11:31 PM, Michael Morris wrote: > Drupal 8 accomplishes this through assert() and a helper class. > > function foo ( array $a ) { > assert( Inspector::assertAllStrings( $a )); > } > > This could be improved by having an collectionof operator similar to the > instanceof operator. > > function ( array $a ) { > assert( $a collectionof string ); > } > > I say "collectionof" because while arrays are the most common traversable > objects, they aren't the only ones. The above approach, combined with the > existing assert structure, can provide the dev time checking of the code > while in under development, and it can be turned off in production. Or it > can be used outside of assert to be checked at all times. > > Since this invokes a new keyword I think that would mean this solution > would be PHP 8. > > Brackets might be used with instance of to notify it to traverse down one > level maybe?? > > function ( array $a ) { > assert( $a instanceof [string] ); > } > > This avoids any BC issues, but it looks odd. Not as odd as \ for a > namespace operation :P But odd. While I normally strongly agree with supporting all traversables, not just arrays, in this case I don't think it works.  The whole point of using a traversable is that you don't have all of the values up front.  If you did... you'd just have an array.  So checking "all values" of a traversable in one point in time is a destructive operation as it runs out the iterator, which if it's an infinite iterator could, erm, be bad. Rather, arrays can/should be checked at once (as above), whereas a traversable should be able to declare that it only returns a given type, and if it ever tries to return a different type then the traversable itself type-errors. --Larry Garfield