Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112660 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 38338 invoked from network); 30 Dec 2020 14:14:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Dec 2020 14:14:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 735D81804A7 for ; Wed, 30 Dec 2020 05:49:35 -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=-0.2 required=5.0 tests=BAYES_20,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-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (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:49:34 -0800 (PST) Received: by mail-lf1-f46.google.com with SMTP id y19so37587176lfa.13 for ; Wed, 30 Dec 2020 05:49:34 -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=Bv22CDy1dL+hvJ/9FSiCcjgKOu13OUdkVM8V+nz5WZY=; b=LSLxuuIs8KWL8YJalOjFHsYXqbvjJZcqsocZYZLUnRdRRuuN6A970bPmd1YNGzOu/1 1guhmMx1BRSjsg1NBGi0RW8cX4IsqDuh7NZJB9W9NIVcaFAUyRPtKAjD2jSqeuoscJlV C3mkDNC9Zx4YBapmvioJbCXUei0PoVi/qAF+GiLiQJmP6MBIrszEB/boFaDyg6GA2Dbm pEgdfkrXtsDVVdcrQmvjHpdH5PzCsiEMsyhX+bz36bv+zNgAMieI5wAf7lAeh0iyHW5g r+a+QKobxdjFz6qw9fevIjiY03MHguInWKEpVrltcGut1E4p+fwQ39BBBEseC0o+48y6 gpTQ== 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=Bv22CDy1dL+hvJ/9FSiCcjgKOu13OUdkVM8V+nz5WZY=; b=lWDQvTAso9RZYa+8ySLG4sEDvWw5VxQ6eR731y/m9DUEsIg/49dpCCS1EPi+GXj7n4 VXmkjbfa+3yqm+OX5pOAW8Xo1NzJ56niJv45t2TKGyYEIA+tD8kSRWh0sds+XuyVGjZu kclg17bPZePz5JF8e0df/Cryfsd2URnwNmR05aGDe9O1Nt733kSy5IIJf1OKeYZZfLRf aI+uDTpwt1v4DHN5mk5whrXpna8tvAttwugastS7chgDuwQlNHeG42ioW0uFgkyNFZHE e9HsWuZV936WF77KjdlRcSv1+0XO+E/nyQzIAJwrybgOEXHm/Hspgp50MYVrKQuzkAs/ yrYw== X-Gm-Message-State: AOAM533VsrYcPdJU2UQhj85XpowG5dGG6ZAIQyMO8HT/5EeRyajADihT whESXPJ8zxuuW/on+wrW4mA1GPrH0i60Z7sgcns= X-Google-Smtp-Source: ABdhPJy9ftncgyw2WB/qdrpBr3D2/zwmeLPCcXH9QOMeDlWc77VwZXMyKH7Z6hwMuVK644s8df94iqkIIUSswVPaKr0= X-Received: by 2002:a2e:9f06:: with SMTP id u6mr23941558ljk.194.1609336172376; Wed, 30 Dec 2020 05:49:32 -0800 (PST) MIME-Version: 1.0 Received: by 2002:ab3:7110:0:0:0:0:0 with HTTP; Wed, 30 Dec 2020 05:49:31 -0800 (PST) In-Reply-To: <459d7ff7-e553-dce9-7d43-c3b1e772e572@gmail.com> References: <1d0abb04-4987-43a9-85bc-bccc3bd6be9a@www.fastmail.com> <03108284-740a-4a5d-130f-15b2e67e9df9@mabe.berlin> <459d7ff7-e553-dce9-7d43-c3b1e772e572@gmail.com> Date: Wed, 30 Dec 2020 13:49:31 +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 21:36 GMT, Rowan Tommins : > On 29/12/2020 10:28, Olle H=C3=A4rstedt wrote: >> I just want to mention that immutability might be applied too >> liberally in the current discourse, and in some cases, what you really >> want is*non-aliasing*, that is, uniqueness, to solve problems related >> to immutability. I think methods like `withX` is an anti-pattern, in >> fact, and a symptom that you do not*really* want immutability, but >> rather uniqueness, at least in some cases. > > > Hi Olle, > > I'm afraid I don't follow what you mean by "non-aliasing" and > "uniqueness" here. Could you clarify, perhaps with some examples? > > Cheers, > > -- > Rowan Tommins > [IMSoP] > Wikipedia has an article about aliasing: https://en.wikipedia.org/wiki/Aliasing_(computing) Also relevant: https://en.wikipedia.org/wiki/Uniqueness_type Uniqueness is when you only allow _one_ reference to an object (or bucket of memory). $a =3D new A(); $b =3D $a; // Both $a and $b point to the same place in memory, so you have an alias Uniqueness and immutability solves similar problems (at least in a GC language like PHP): Spooky action at a distance, fragile composition, rep exposure. The are more advanced systems of ownership than just uniqueness, e.g. Universe Types, but let's ignore that for now. https://www.researchgate.net/publication/221321963_Ownership_transfer_in_Un= iverse_Types Uniqueness has the benefit of being more performant than immutability, since it leads to less memory copy (but of course it's not certain this performance gain matters in PHP programs). You can compare a builder pattern with immutability vs non-aliasing (uniqueness): ``` // Immutable $b =3D new Builder(); $b =3D $b->withFoo()->withBar()->withBaz(); myfun($b); // $b is immutable, so $b cannot be modified by myfun() return $b; ``` ``` // Uniqueness $b =3D new Builder(); // class Builder is annotated as non-aliasing/unique $b->addFoo(); $b->addBar(); $b->addBaz(); myfun(clone $b); // HAVE TO CLONE TO NOT THROW EXCEPTION. return $b; ``` The guarantee in both above snippets is that myfun() DOES NOT modify $b before returning it. BUT with immutability, you have to copy $b three times, with uniqueness only one. That's why I think ownership system merits some attention, not ONLY immutability. :) Unfortunately, it can take a looong time to force new concepts like these into common discourse. Rust helps, obviously. The language Clean has opt-in uniqueness (I need to read up more on it, tho). Linear types in e.g. Haskell is also related (but is both more complex and more powerful). I would like to add an annotation to Psalm, like @psalm-no-alias, because this is also needed to make type-state sane. But, didn't do much yet, only some brain farting. :) Olle