Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110946 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 53677 invoked from network); 11 Jul 2020 16:21:43 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Jul 2020 16:21:43 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9E5EC180532 for ; Sat, 11 Jul 2020 08:13:26 -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, 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-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) (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 ; Sat, 11 Jul 2020 08:13:26 -0700 (PDT) Received: by mail-ed1-f44.google.com with SMTP id a1so1530273edt.10 for ; Sat, 11 Jul 2020 08:13:26 -0700 (PDT) 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=ajroGk8WskTB61t3Bye6b+uDKHfH53cjM8CJpUcgQPo=; b=hxtVG4d+519u06iUAhEkK4OleX7YLyKAp5iv/QFgPIaEfoJ5yrBSY5P0fH8+5RImPC jQcef1Ygmt72tkcNH1Kjzb5BAooT4n72gMuKHViBcgebO7nVS6ww67TK2DnUuE0vcfLZ OQC0DX8Ex0O2FzMYF136tz/6X1dPA/WnPBWhHnF6bjKD2ZWZKg89wlk+i5S4gQLReTz6 MPJ6Q5G+MBumFWJmDPXJ5/U6oI67yyaE53tK5m9h4R46TADD7AgoxP9v4Fv5BJM5e+N8 FUIv+V9B1eiS4X47W2b9Y6t96w4GnK5n2QRnoURy8IXJ+GGt+NA3DJqT5kIkP/KNU0tL Z5ww== 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=ajroGk8WskTB61t3Bye6b+uDKHfH53cjM8CJpUcgQPo=; b=pzsG0hEJMxNnE/CdSC+eV0IkrXtOI1vxQ3vglD8q1oq/JyVGTjEPhrfDJgRbiVV0lE WvnaKbBwWEjAzSAAituCoaDmp4MgdKKtePVfJpHhqN1aGD6JtoM8hVf4HQZCJr+lqbAE TiF+HxYXcRNVJ9nEkiLpSPVAh0l07DRlkWxR9BJJIasjINx7T6WRS5kwsEb8bCKStt5M dc5YPTEhgcQdQwqIh2V9FcaeCAuZKqFGibysiIVUin+qW7d5ZxZkFVrIG0Qzy+oDsKCs hwEkMA9cQ3U2G8zXFGtkbYQAQvDbQMJEGwktjT9gnJUMDXaozRLzEuZwfbXlv/23s1bz 2FmQ== X-Gm-Message-State: AOAM531I3FPp5woBMAgra2YxjQRgs8vLBwTMzJVbZNLTigJFngJY6cnL hzgof3oPgdbtr9uJbfFuSGXd1JYzAvknGwTca2rMWw== X-Google-Smtp-Source: ABdhPJzt+a/Bae2cdR9ruMHEDftc+p07TvLjrRFtdkhBAJ+DaYz04wlyu/uOvp3JlhzYliO22ZFw5EOOLuAS/fUZbsc= X-Received: by 2002:aa7:d7cf:: with SMTP id e15mr83752518eds.236.1594480404968; Sat, 11 Jul 2020 08:13:24 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a54:3091:0:0:0:0:0 with HTTP; Sat, 11 Jul 2020 08:13:24 -0700 (PDT) In-Reply-To: <6D442090-FA51-48A9-A07D-F27F5EF6FC0A@gmail.com> References: <6D442090-FA51-48A9-A07D-F27F5EF6FC0A@gmail.com> Date: Sat, 11 Jul 2020 15:13:24 +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] Possible RFC: UniqueInterface that throws exception at refcount > 1 From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2020-07-11 14:56 GMT, Rowan Tommins : > Hi Olle, > > On 11 July 2020 15:06:13 BST, "Olle H=C3=A4rstedt" > wrote: >>I'd like to discuss the concept of ownership in PHP, from the point of >>view of a new interface UniqueInterface (or SplUniqueInterface or >>something), which will throw a UniqueException if refcount > 1. > > It's an intriguing idea, but I'm not sure how well it would work in > practice. Maintaining a strict refcount of one means never passing the va= lue > to another function or scope, which seems very limiting. Yes, the point is to be limiting. :) Just like with immutability, you apply certain limitations to avoid access abuse. > > It would also run into the usual problem PHP has that everything is > determined at run-time, so every time you pass an object anywhere, the > engine would need to check it wasn't marked in this way. That could be > avoided if instead of a special type of value, this was a special type of > variable; then it could probably be determined at compile time that you w= ere > passing a "local only" variable to another scope. > > >>Use-case: Mutable objects that cannot be shared (without (deep) >>cloning). > > This is another tricky aspect - like enforcing deep immutability, it impl= ies > some recursive constraints which are easy to enforce for scalars, but tri= cky > once you look at things like arrays, which can contain references, object= s, > other arrays, etc. > > > As described, the user would have to manually perform a clone whenever th= e > object was passed anywhere, even if they weren't mutating it anywhere. If > you let the language do that automatically, you can use an automatic > copy-on-write system, like PHP already does for arrays (and, funnily enou= gh, > PHP 4 did for objects). > > Alternatively, you can make the object purely immutable once it has passe= d > out of its initial scope, or once a special function has been called to > "freeze" it. This would be an interesting solution to the "modified clone= " > problem of full immutability (how to implement withFoo methods), since an= y > "frozen" object could be cloned to get a mutable copy, which would be fro= zen > before passing on. > > Regards, > > > -- > Rowan Tommins > [IMSoP] Regarding "freezing" an object, I actually already wrote about this in a blog post, using dependency injection as a use-case, and a trait to add a method to check the refcount: http://olleharstedt.github.io/php/2020/07/08/immutable-at-borrow.html