Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101665 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64132 invoked from network); 25 Jan 2018 16:15:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Jan 2018 16:15:33 -0000 Authentication-Results: pb1.pair.com smtp.mail=bishop.bettini@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=bishop.bettini@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.48 as permitted sender) X-PHP-List-Original-Sender: bishop.bettini@gmail.com X-Host-Fingerprint: 209.85.213.48 mail-vk0-f48.google.com Received: from [209.85.213.48] ([209.85.213.48:43396] helo=mail-vk0-f48.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9A/80-61119-3A20A6A5 for ; Thu, 25 Jan 2018 11:15:33 -0500 Received: by mail-vk0-f48.google.com with SMTP id x203so5162014vkx.10 for ; Thu, 25 Jan 2018 08:15:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:reply-to:sender:in-reply-to:references:from:date :message-id:subject:to:cc; bh=TZHHfXMl/CIZPQzXJaFvYSbs47Xo2OD2Nt0nx+IFA8w=; b=IFICn6Kb9oRGKyiygL4sagAWrNdJ5e9arjdkZ9ZGsZpvrbiFaJBcRKSzA6Wm0rQtxe i8jkf1D7Jkuhdj5DjO1rswOVPRPKqcreIVPQNRa5ljHuhf+zNNqhY+3inBFuxDuNaqz4 fXdC5iwzdolfMfJLTNk1lWJnd/SVl+YUJQE8axYhe2Bqb51ixzrj1QABMfiOSGspfMGj VxxTE6cjKNUCZ7ukC0prp24ou0bkk6XRXDU0sGvUwbc9c569dhCi51rsXAAyxnMpA9VD 9oRBo2tgDtM5ERI9ChXVDFO67QFUIx+Td7j8nlNzXpdupvPZhGAvUJX0d+4RhljqYpS0 S5vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:reply-to:sender:in-reply-to :references:from:date:message-id:subject:to:cc; bh=TZHHfXMl/CIZPQzXJaFvYSbs47Xo2OD2Nt0nx+IFA8w=; b=rM3oQsw41WQ8RjHtAjXTRrh77oqj51lzoctfgCxRonMw/xhRvpXo7Qo7nr0Lj/Xotu jT6uu71R/5LvSfmjCY2aMjCTSgds72L1oK/gQjnxBnopF2CkKCG1m5+bkosL6bTkZVAe 8KunCcY2+iaQjIdmLvBsxhOXdSZ3+qTxyz1tEdpL7zOgb6L6hhKT/ntJF9jHzl0CnhEB 97djA7PVzdZ6I90ETlnAFV3YTV2DQH+7zmBOF+EYEpX/azMpIvBTuPG0WGPJqNljfHY+ 7TreJuZvLnrRusLY8D1DDZdoRf9I0kEtx5PJ9sn5uEcq0/n+p2ws7abWngwZmh53FWIy K6Qw== X-Gm-Message-State: AKwxytdcUSrrocaLG4T/zNfbp5OZrePga1pVQSsSIqCtQBlBsVEIz2xh JkglF88dJV7zgut/34sBQVYIAx33RjOYvkWoVDg= X-Google-Smtp-Source: AH8x225VO/e2WHlAdDPgEuAopvOlhKU/r9nFd5f9wgKt8uawG8YHr+xjPn+8kyUb+fYvOUP8BMG0ty3GGehcarO/bH8= X-Received: by 10.31.163.216 with SMTP id m207mr7993633vke.63.1516896929327; Thu, 25 Jan 2018 08:15:29 -0800 (PST) MIME-Version: 1.0 Reply-To: bishop@php.net Sender: bishop.bettini@gmail.com Received: by 10.103.54.156 with HTTP; Thu, 25 Jan 2018 08:14:59 -0800 (PST) In-Reply-To: References: Date: Thu, 25 Jan 2018 11:14:59 -0500 X-Google-Sender-Auth: 4MuJhJQVcb0CIS-flRZVqX1JFDs Message-ID: To: Derick Rethans Cc: Michael Morris , PHP internals Content-Type: multipart/alternative; boundary="001a11425e70a22fd005639c16b7" Subject: Re: [PHP-DEV][RFC][DISCUSSION] Collection Inspection From: bishop@php.net (Bishop Bettini) --001a11425e70a22fd005639c16b7 Content-Type: text/plain; charset="UTF-8" On Thu, Jan 25, 2018 at 9:52 AM, Derick Rethans wrote: > On Wed, 24 Jan 2018, Michael Morris wrote: > > > Ok, here's another idea I've been mulling over. This I know is possible > > because I've done it using user land code, specifically Drupal 8's > > Assertion\Inspector class. > > > > https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Comp > onent%21Assertion%21Inspector.php/class/Inspector/8.5.x > > > > These methods provide a means to inspect collections - arrays usually but > > also Traversables. They fill a hole in the PHP library - the ability to > > check collection integrity. > > IMO, it makes a lot more sense to check integrity when creating the > "array" structure. Instead, I would suggest to add a native Collection > type, that takes a "type" as argument. They aren't quite full generics, > but it does 1. fix something; 2. isn't really complicated. > > What I am suggesting is to add a new syntax "Collection<$type>", > mimicking a class, but having a type as "argument": > > $a = new Collection; > > And then $a can act as if you'd use an ArrayAccess'ed class. > Upon each set or update, the type of the value can then be checked > against the type. > Agreed, and we can get *almost* there today with: $collection = collection_of('is_int', [ 1, 2 ]); $collection[] = M_PI; // exception thrown given: function collection_of(callable $validator, $input = []) { $collection = new class($input) extends \ArrayObject { public function offsetSet($index, $value) { if (($this->validator)($value)) return parent::offsetSet($index, $value); throw new \DomainException; } }; $collection->validator = $validator; foreach ($input as $key => $value) $collection[$key] = $value; return $collection; } > Consequently, this would also mean you can type hint on Collection > instead of for example an earlier suggested array of type, where upon > passing in the array each member was checked for its type (slow). > Regrettably, class_alias cannot quite work here, because the validator is passed at run-time, so making this part of the language seems a good place to go. Cheers, bishop --001a11425e70a22fd005639c16b7--