Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112659 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 33855 invoked from network); 30 Dec 2020 13:26:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Dec 2020 13:26:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 709B41804C6 for ; Wed, 30 Dec 2020 05:00:55 -0800 (PST) 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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (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, 30 Dec 2020 05:00:54 -0800 (PST) Received: by mail-lf1-f50.google.com with SMTP id o17so37477452lfg.4 for ; Wed, 30 Dec 2020 05:00:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=9/v5L41y+zDTcVhZUkore7JmJDhGuIDaiasvlzrBeXo=; b=jhIvDZI2HjJXdUSqxfwRo00Dx2AnijHmdCv8c5UfROKHeoqwyjxFBAARisVVhMC7A4 0W2pWGyFFwwIb0nYdls4n+qps24KMnfLgdh8P6H7btVIi6hRSoBs7U3JFUtEW/7LJCuH ov/lXRMgRjauPhEa3Qb7QymUEaY6drAmMGwFJVC1XDQWGYFMxR1yJ4Xd0wC9Lnq77r8f dk90ss9bYiMpFzUH8MECbhnd5TCb1x/mH14hd2TEra27MqxXx0bxNjeC/QSUynVqrOSh llH5OEwqLtS5JSxAPLxG3XJnGZ3CZJqb13uFIV8bat6czZ6S6VZ/Vbq/7J+H1T/Oerpt RwCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=9/v5L41y+zDTcVhZUkore7JmJDhGuIDaiasvlzrBeXo=; b=N9EvD+47Z51cJwCcTUGBlng7X1Aq2z7qTF+KnbMATuB+tRD+PwLNQxgqaSXdNxNAnN 50ae3pfK2jSXqUwWMWkIc9BfkUpA39AzvxYBFom2bRQj5JYvKGawxX0xXEZ+GXJJbQ+B p3l9vGYQqRPvap54rz4YZoZRii+YPXKVZhWKndxeAg0lLAa59PKBE4Y9wX2RGUb8fe9a dBakl1sEZgNd3sW+nOVfkelV+93CYszMelEjFu1txFzpaToPPGzip+grrHYMCVsgp21Z D5xrjNLMxgUNrZ1NgjuHzpVls9Juu9sDAstApAuTriFgXSgqFkMs+2C8uZiVGA5PNXbw XUPw== X-Gm-Message-State: AOAM530wVr8vI56OHRmTK/WO4meGzB01cQcFcWYPw3LPzBnKF5cszgf1 yAe238i9CnC3Kq0mGIyBvNNIqZPj8ERjqbbxKB4= X-Google-Smtp-Source: ABdhPJxMUkWE8nGa+QnTCZlzMXDKSGEy1AK0PCxN5/Obt+J0hqNfQd4AMO0rFwKTd840mEKk6o266cNnm2OTZqEzvuY= X-Received: by 2002:a19:cb05:: with SMTP id b5mr18367511lfg.61.1609333252308; Wed, 30 Dec 2020 05:00:52 -0800 (PST) MIME-Version: 1.0 Received: by 2002:ab3:7110:0:0:0:0:0 with HTTP; Wed, 30 Dec 2020 05:00:51 -0800 (PST) In-Reply-To: References: <1d0abb04-4987-43a9-85bc-bccc3bd6be9a@www.fastmail.com> <03108284-740a-4a5d-130f-15b2e67e9df9@mabe.berlin> Date: Wed, 30 Dec 2020 13:00:51 +0000 Message-ID: To: Rowan Tommins Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Analysis of property visibility, immutability, and cloning proposals From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2020-12-29 22:43 GMT, Rowan Tommins : > On 29/12/2020 18:38, Olle H=C3=A4rstedt wrote: >> Instead of shoe-horning everything into the PHP object system, did >> anyone consider adding support for records instead, which would always >> be immutable, and could support the spread operator for cloning-with >> similar as in JavaScript or OCaml? They could be based on PHP arrays >> and thus be passed by value. > > > While we could create a brand new "record" or "struct" type, I think > there are a few reasons to think it would end up *looking* more like > objects than arrays: > > - we have an established syntax for declaring types of object (class Foo > {...}), and none for declaring types of array > - the 'bar' in $foo['bar'] is an expression, implying dynamic options; > the bar in $foo->bar is a bare identifier, implying statically defined > options > - similarly, we have a syntax for creating object instances, with > statically analysable members: new Foo(bar: 42) > > > The spread operator could be made to work with either style, if we > preferred it to using "clone ... with ...": > > - ['bar'=3D>69, ...$existingFoo] > - new Foo(bar: 69, ...$existingFoo) > > > However, arrays arguably already have a clone-with syntax, more normally > thought of as "copy-on-write". Rather than "mutable with special logic > to pass and assign by value", I think you can model their behaviour as > "immutable with special logic to clone with modifications": > > $foo =3D ['bar'=3D>42, 'baz'=3D>101]; > $newFoo =3D $foo; // lazy assignment by value is indistinguishable from > assignment by pointer > $newFoo['bar'] =3D 69; // $newFoo is a modified clone of $foo > $newFoo['bar'] =3D 72; // mutating $newFoo in place is indistinguishable > from creating and assigning another modified clone > > > In theory, "records" could have this ability with object-like syntax: > > $foo =3D new Foo(bar: 42, baz: 101); > $newFoo =3D $foo; > $newFoo->bar =3D 69; // $newFoo is a modified clone > $newFoo->bar =3D 72; // can be optimised as in-place modification, but > conceptually cloning again > > > In the simple case, that's equivalent to a clone-with: > > $foo =3D new Foo(bar: 42, baz: 101); > $newFoo =3D clone $foo with { bar: 69 }; > $newFoo =3D clone $newFoo with { bar: 72 }; // can probably be optimised > the same way as the above examples > > > It would allow more complex modifications, though, such as deep > modification: > > $foo =3D new Foo(bar: new Bar(name: 'Bob')); > $newFoo =3D $foo; > $newFoo->bar->name =3D 'Robert'; > > That last line would do the same as this: > > $newFoo =3D clone $newFoo with { bar: clone $newFoo->bar with { name: > 'Robert' }}; > > How desirable that is, and how it fits with the use cases in Larry's > post, I'm not sure. > > > Regards, > > -- > Rowan Tommins > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > Good breakdown. One benefit of records is that they can be structurally typed (instead of nominally, as classes are), but that's probably never going to happen in PHP. :) Perhaps a `readonly` attribute is best for now? Compare with the annotation supported by Psalm: https://psalm.dev/docs/annotating_code/supported_annotations/#psalm-= readonly-and-readonly Olle