Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109788 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 85245 invoked from network); 22 Apr 2020 20:17:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Apr 2020 20:17:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 979EE1804B7 for ; Wed, 22 Apr 2020 11:49:32 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-vk1-f176.google.com (mail-vk1-f176.google.com [209.85.221.176]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 22 Apr 2020 11:49:32 -0700 (PDT) Received: by mail-vk1-f176.google.com with SMTP id p123so983146vkg.1 for ; Wed, 22 Apr 2020 11:49:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=FVwqNb3GgwVZAoSO9/C4rHObfUXY1KR0m25FWEU/JO4=; b=meBvkZ0kiqT6jU+/h4SlJ9V3CAn5GkNzSeRt0eHzl9iaTmhHtz8i0bgJirrH+dunPZ GQgJJL3p/02ySOeC2GXZcyTvh0DTojrvu4o/VhzSJ1/OgyS7sEKP/pS1uhsTsWeue/8s pPcWLEakKdnLthn7pehPSvxZFympil2olKXIri8vUnaL7bHQ+b58LRN4ooUExpxqhRll e3tBe0vr0aw+MFMxHh2hzbQJvgtBF+F9SnHZjf1W3dyjUwFGoczRB6WnZCj0khVi0lhl Rz5St62TUDat0SLHKc8oapzq9JnBsZt0ookt2XoN4hoxEDLCWA4BkRKZ99HTzKnbxFbd pYOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=FVwqNb3GgwVZAoSO9/C4rHObfUXY1KR0m25FWEU/JO4=; b=uOxe4VIrqsGaNEjk/Ss5Ic22ZodDcoL9kTPsMd8nNfNs44jnmOliwNeiqyB701yH8T l3JGrYcz9KHNyg1JHr9cMpQP2f1By+lcGrMfDHYCp99ezdERSQDDC+J4B+l0qw7jvywH mh8PBAJ6/Q4egnSckkRp4Cp2m+PpQwvS7iPVH/FFQ87WI2QiUnaXsVxfB0g4mAfBj66V M0F4IbmqQ5JLGvcjC8WFipQCaEw9ZlNIORrdme2mAe+14QEvpYSq58yUpNtNHc7mGbXQ XlNjJHmZ9ofahl7+cxJeEp/D0Yf36f42QVRQq2LH684GF9dF/+Rju3+1nji3cgm9USY/ mtkA== X-Gm-Message-State: AGi0PuZoqN2qpMW5viKTf2Szx4LE4wKnJ1nwYnW6O+QMYuMANOQYlxmc Y6HfZWxEBX+eY0UGuh+9HVBQZxVDDug+Q3aDl53/FaHK X-Google-Smtp-Source: APiQypI7QGiZ45gavTPPZPiIapLIxJEzVJ7/n94NIpzy2W8/3lth6v3E27SHIlrtA+5Cxg0LmRS7Pjg3VZxp2c5N/mY= X-Received: by 2002:a1f:a844:: with SMTP id r65mr416162vke.56.1587581370540; Wed, 22 Apr 2020 11:49:30 -0700 (PDT) MIME-Version: 1.0 References: <4c1cd016-1e41-4ebc-afc4-d8d3cd1c319b@www.fastmail.com> In-Reply-To: <4c1cd016-1e41-4ebc-afc4-d8d3cd1c319b@www.fastmail.com> Date: Wed, 22 Apr 2020 14:49:17 -0400 Message-ID: To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="000000000000a4a88a05a3e59744" Subject: Re: [PHP-DEV] Any interest in a list type? From: matthewmatthew@gmail.com (Matthew Brown) --000000000000a4a88a05a3e59744 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable > Is it an effective subtype of array? I was thinking it should be (with the auto-conversion mentioned above), but I can see a compelling case to not have the auto-conversion when manipulating =E2=80=93 while it would bloat the stdlib a little (we'd need = a whole bunch of list_* functions) the separation would simplify things a lot (e.g. list_filter would return a list with sequential keys, whereas array_filter returns an array with possibly non-sequential keys) And then you could cast between the two like "(list) $some_array" (which would preserve order but remove keys) and "(array) $some_list" as necessary. There could even be some automatic list <-> array casting when calling functions not in strict_types mode. > Should they pass like arrays or like objects Definitely like arrays =E2=80=93 I want them to be a drop-in-replacement fo= r people who currently use arrays in places lists are more appropriate. > Should they be mutable or immutable Definitely mutable, like arrays are > Are they iterable Yes, the keys are sequential integers > Does it make sense to add them without type enforcement via generics Absolutely =E2=80=93 this shouldn't be tied to generics landing (which I'd = imagine is a PHP 9 feature at this point, whereas this could be a PHP 8.x feature). > can they be mapped/filtered/reduced Absolutely =E2=80=93 I think we'd have list_map, list_filter, list_reduce f= unctions to provide that functionality. On Wed, 22 Apr 2020 at 13:28, Larry Garfield wrote= : > On Tue, Apr 21, 2020, at 2:00 PM, Matthew Brown wrote: > > Before I create an RFC or attempt a reference implementation, is there > any > > interest in adding (and then obviously supporting in perpetuity) a list > > type? > > > > The first line of PHP's documentation for "array" states: "An array in > PHP > > is actually an ordered map". There are some optimisations that make > arrays > > with ordered integer keys faster, but I'd be interested in a separate > type > > dedicated to *sequential* integer keys. Such a type would presumably > > consume a little less memory. > > > > Why "list" and not "vec" or similar? "list" is also a reserved word =E2= =80=93 I'm > > imagining that you'd construct a list like > > > > $l =3D list["a", "b", "c"]; > > > > I imagine such a "list" type would be a subtype of "array" =E2=80=93 ev= erywhere > > that array was accepted, a list would be also, and it would have the sa= me > > copy-on-write behaviour. > > > > If people are interested in having that type, there's a question of wha= t > to > > do with > > > > $some_list["a"] =3D 5; > > > > Would you convert the $some_list to an array, or throw an exception? > > Converting to an array would seem the more PHP-like thing to do. > > > > Similarly, the behaviour of > > > > $some_list[$key_out_of_current_range] =3D 5 > > > > would be a matter of debate too =E2=80=93 would that turn $some_list in= to an > array, > > or would it fill up any preceding entries in the array with null? > > > > What other questions are there? Is a "list" type even a good fit for PH= P, > > given most of its users seem pretty content with the current > > swiss-army-knife array type? > > Most users don't realize that PHP's arrays-not-really-arrays have caused > millions of dollars in security breaches in the past. :-) They're > dangerous and to be avoided whenever possible. > > I'm very open to a list/sequence type, but as others have noted there's a > whole crapload of details to sort out to make it viable. In particular: > > * Is it an effective subclass of array? IMO, no. It should have > absolutely no auto-conversion to/from an array whatsoever of any kind, > period. Keep them as separate as possible. > > * Should it even have random-access indexes? Honestly I'd say no; Just > support adding, removing, and iteration and generate the indexes on the f= ly > when iterating if necessary. > > * Should they pass like arrays or like objects? Many questions here. > > * Should they be mutable or immutable? I could argue for either one > effectively, I think, though I'd honestly favor immutable. > > * Are they iterable? Presumably, but does that have any weird > implications for iterables that implicitly assume there are keys? How's > that work? > > * Does it make sense to add them without type enforcement via generics? > Lists + Generics would be lovely, but as we've seen Generics are Hard(tm) > and Not Imminent(tm). But would adding them now make a generic version > harder in the future? (I've no idea.) > > * Besides add/remove/iterate, what other baked-in functionality should > they have? Eg, can they be mapped/filtered/reduced? It would really suc= k > to revisit lists and not fix that disconnect in the API. (Insert me > talking about comprehensions and stuff here.) Ideally this would happen = as > part of a larger review of how collections work at various levels, which > are currently highly clunky. > > Those are all solvable problems (and I've likely forgotten several), but > they would have to be thought through extensively before an implementatio= n > could be viable. > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --000000000000a4a88a05a3e59744--