Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112650 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 68386 invoked from network); 29 Dec 2020 23:08:53 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Dec 2020 23:08:53 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8025D1804DC for ; Tue, 29 Dec 2020 14:43:24 -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-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.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 ; Tue, 29 Dec 2020 14:43:23 -0800 (PST) Received: by mail-wr1-f50.google.com with SMTP id d13so15938325wrc.13 for ; Tue, 29 Dec 2020 14:43:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=PgsD96lDY/CfkhYaUubbikyBit+qcB5yOriIlXHjUwI=; b=ZDNMiBwhMlwMFYzWtLKByIL281+nJxdxcHKkUS0jw4jVLQ/2ShhH9SjShwLAEzqrL/ H+Kx7BtRlC01RrilMQNRjei1mKUGlq10/yXWeGMzsf9d0IEG173+8sz4hRdfqJLF3rXd urrzjdVZx79D7wpr5pcOyI2RV13HaDD04/OGOKZ+b/r7WHo0VmKzU2R0bG2P0IWN3gKG 7Riac4Zc5t6nUAfwPAUBXTxVIHgqLb/bHxlxIuA2sZ6jJcGytK94TBN+4BRk6DO+Rxd1 ReTLQD/aA7pMvjUaIeQrnz/mBgxY0XaT/DlRSpxpcZv0GUAZ5G8wTmt6vTjjQNXTZiV3 Cf+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=PgsD96lDY/CfkhYaUubbikyBit+qcB5yOriIlXHjUwI=; b=RZ8/vbbIM+xRr6GWPp60DCC2jC3TMa96LpRWRJiUHKNnv/JX4C0L0b2pmUptqmC4Ru 9chHRUKP3HEW4iyP590DI/umcxj8rg8c9I88dWtg5n8fKUKB4Cz1XwdSpvzYdoAHe11D /cKYXPbFqrDigHC9T/JNcbR84s2/GyWiorxoxWaP/k9eRAsiu+UmwmcV2T/pH+HC7rFK HvYx8pfVdfN6q+YehreapTQtrKeaxha22E/DIAQIvcvjTNtYYZQyQRaeGeHLazZPLCXr AlUDa/4IgFGM6T9WUoywbmJer3pJH0TsPPT3b1adkTqHINDxPLY5plhwFw85c9YK+7wx xpwQ== X-Gm-Message-State: AOAM531yeRd76YAKBz5wcQltjrrkBluRRzQrkuc5zpD+fflJgjT/+oqk WcDr5cxssPQgBv6HD74eyuzFx1/ZSXG05g== X-Google-Smtp-Source: ABdhPJwYwfq9tM+S1mZdolwtRUn4/kGuhG7C4sZzfji1ybyXGgSrnNTfC4KM3RqHDLT0E17jKde5+A== X-Received: by 2002:a5d:6ccc:: with SMTP id c12mr57889388wrc.4.1609281800482; Tue, 29 Dec 2020 14:43:20 -0800 (PST) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id o3sm49283853wrc.93.2020.12.29.14.43.19 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 29 Dec 2020 14:43:19 -0800 (PST) To: internals@lists.php.net References: <1d0abb04-4987-43a9-85bc-bccc3bd6be9a@www.fastmail.com> <03108284-740a-4a5d-130f-15b2e67e9df9@mabe.berlin> Message-ID: Date: Tue, 29 Dec 2020 22:43:18 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] Analysis of property visibility, immutability, and cloning proposals From: rowan.collins@gmail.com (Rowan Tommins) On 29/12/2020 18:38, Olle Härstedt 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'=>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 = ['bar'=>42, 'baz'=>101]; $newFoo = $foo; // lazy assignment by value is indistinguishable from assignment by pointer $newFoo['bar'] = 69; // $newFoo is a modified clone of $foo $newFoo['bar'] = 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 = new Foo(bar: 42, baz: 101); $newFoo = $foo; $newFoo->bar = 69; // $newFoo is a modified clone $newFoo->bar = 72; // can be optimised as in-place modification, but conceptually cloning again In the simple case, that's equivalent to a clone-with: $foo = new Foo(bar: 42, baz: 101); $newFoo = clone $foo with { bar: 69 }; $newFoo = 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 = new Foo(bar: new Bar(name: 'Bob')); $newFoo = $foo; $newFoo->bar->name = 'Robert'; That last line would do the same as this: $newFoo = 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]