Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:95702 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 6787 invoked from network); 6 Sep 2016 18:33:47 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Sep 2016 18:33:47 -0000 Authentication-Results: pb1.pair.com smtp.mail=marijic.silvio@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=marijic.silvio@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.51 as permitted sender) X-PHP-List-Original-Sender: marijic.silvio@gmail.com X-Host-Fingerprint: 209.85.218.51 mail-oi0-f51.google.com Received: from [209.85.218.51] ([209.85.218.51:33372] helo=mail-oi0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C2/11-18051-90C0FC75 for ; Tue, 06 Sep 2016 14:33:46 -0400 Received: by mail-oi0-f51.google.com with SMTP id y2so86993532oie.0 for ; Tue, 06 Sep 2016 11:33:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=VFhHB98yZNBlsKsfggKJgjOZJnKhV5WpsBhtqm0o8wU=; b=YSF4XXUg0cRZkdta+dPR4dLjk0x3xU357f2+5rjamjyIa7SeT6Z3o2aDE0m6Jb+0My xc9VvfGVfm5WQh4cqKggnrVLiZjblCY91dhpshHv/gRzMGTUR+Yt4uUI4eeNt/P6Ww8B hWiK2qa4TWJZVbSKQW65WLpBDGDFa4wGQva9YTWIryVsBuX/R694nZhlrogGbW2XQ5WZ KO3t4lmvmITxjpRzqRUYWPFt4MgmDOrmkNESQNBK5LHPlDpDMncicMtF8UO+bfpAkYuJ PfB5nobrZgiMjW8xm208cJqiFN1V2bSoCWqiSImjCK0aBrx3hpao6+ffQmoF1enRFF6i Jn/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=VFhHB98yZNBlsKsfggKJgjOZJnKhV5WpsBhtqm0o8wU=; b=EEUfJ3W2FXxoCe2HH5AaT0As2sKpZDVWKPTJDWsqQWbFqI9hS3BjUc0W8ge6X2muQI vTlm1G2X9DkwW1KoIiqbn+85wo7u6b45fNNWEHT9UjTNPCbVF4Ebd4WCevuDj81XrO4z DFSp5+JF6K49VJ+ol7nKS36TJhOr05D8boz3P61/0sj1ec1EE0rMiVseU/P4aA77uqkq Dgwe92fMynGbd2RjdczvlaD5cr1QjeJWWcbb75vBbVKqYJHco8huiVlTk3HEUBSmULRw PMYD1XhlVIa6nJtWQdIaxdDFrydfhLCz3b83px/eRdSiJy2zeURYaiCftsKv7Q4QwP2U mLbA== X-Gm-Message-State: AE9vXwMlwty05QrYquxH7DQVueUXWM0bhOhX/y9vs4tRKPC/V177+cs9gjLbp6kUKvgvs6S6H/xH/3hhil5RvQ== X-Received: by 10.36.253.3 with SMTP id m3mr209668ith.6.1473186823213; Tue, 06 Sep 2016 11:33:43 -0700 (PDT) MIME-Version: 1.0 Received: by 10.36.237.74 with HTTP; Tue, 6 Sep 2016 11:33:42 -0700 (PDT) In-Reply-To: <83fa661e-2d3d-6548-a506-fb969be31c0e@garfieldtech.com> References: <0e71d28e-1d64-5372-b58d-e54c7afae3b8@fleshgrinder.com> <642a6e78-90ea-cbf0-ec1c-376c24e568c5@fleshgrinder.com> <0800a5ca-3d14-c541-1a1a-2574ec802b8c@fleshgrinder.com> <83fa661e-2d3d-6548-a506-fb969be31c0e@garfieldtech.com> Date: Tue, 6 Sep 2016 20:33:42 +0200 Message-ID: To: Larry Garfield Cc: PHP Internals List Content-Type: multipart/alternative; boundary=94eb2c11ce52492856053bdb09c8 Subject: Re: [PHP-DEV] RFC - Immutable classes From: marijic.silvio@gmail.com (=?UTF-8?Q?Silvio_Mariji=C4=87?=) --94eb2c11ce52492856053bdb09c8 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable @Larry, Fact that $this in transformer method represents clone and the original object was my biggest concern, but again if you look at t from a point of view where that method describes transformation it doesn't look that bad. My initial idea was to introduce copy() method which would take as a first argument immutable object and as a second array of parameters which are going to be changed. If we decide for transformer method as a solution, I don't think it has much sense to allow cloning immutable object from user land. I think we can live with the fact that they are mutable only in a controlled way. But the fact that some solution for cloning is here is a great deal since that was issue from the day 1. Cheers, Silvio. 2016-09-06 18:01 GMT+02:00 Larry Garfield : > On 09/05/2016 11:37 AM, Fleshgrinder wrote: > >> >> On 9/5/2016 10:26 AM, Micha=C5=82 Brzuchalski wrote: >> >>> I had a talk at Room11 and we discussed idea of `mutator` keyword. >>> There were some concerns using `mutator` as a keyword - that's because >>> immutable object is not being muted and also magically appeared `$clone= ` >>> would be confusing. There's an idea of creating clone before function >>> begins >>> and operating simply on `$this` while it's newly created clone from >>> immutable >>> object instance and the additional keyword for such method would be for >>> eg. >>> `transformer`, so basically it may look like this: >>> >>> immutable class Money { >>> public $amount =3D 0; >>> public $currency; >>> public function __construct($amount, $currency) { >>> $this->amount =3D $amount; >>> $this->currency =3D $currency; >>> } >>> public transformer function amount($newamount) { >>> $this->amount =3D $newAmount; // $this actually is newly creat= ed >>> clone >>> return $this; >>> } >>> } >>> >>> $oneHundret =3D new Money(100, $eur); >>> $twoHundret =3D $oneHundret->amount(200); >>> >>> How about that? >>> >> The thing about mutator is only partly true because setters are >> generally just called mutator, it does not state anything about how it >> mutates something. Note that the keyword might proof useful in >> nonimmutable classes too for other use cases in the engine. Hence, I >> would not throw it off board just yet. >> > > Naming things is hard. :-) While working on PSR-13 for FIG, we ended up > with the name "evolvable" for interfaces with the withFoo() methods. I a= m > not necessarily endorsing that as the best term (we weren't super happy > about it but it was better than the alternatives), but providing it as a > data point. > > > It is true that $clone might come as a surprise and that's why I >> proposed to pass it as the first argument to the mutator function. >> However, you were right that that is a suboptimal proposal (and it >> pretty much goes completely against my previous paragraph). >> >> However, always providing $this as a clone is also not a good idea >> because you might want to return the same instance. This really depends >> on the kind of action that is desired. >> >> It's probably much simpler to keep the clone requirement and unseal the >> clone while making the __clone method protected by default. >> >> immutable prototype { >> >> protected function __clone(); >> >> } >> >> immutable class Money { >> >> public $amount; >> >> public $currency; >> >> public function __construct(int $amount, Currency $currency) { >> $this->amount =3D $amount; >> $this->currency =3D $currency; >> } >> >> public function withAmount(int $amount) { >> if ($this->amount =3D=3D=3D $amount) { >> return $this; >> } >> >> $clone =3D clone $this; >> $clone->amount =3D $amount; >> >> return $clone; >> } >> >> } >> >> This might seem like we haven't achieved much compared to the current >> state of affairs but we actually did: >> >> 1. No introduction of additional keywords (besides immutable) >> 2. No surprises (magic $clone variable) >> 3. No hard limitation on cloning (but disallowed by default) * >> 4. Full freedom to developers when to clone >> 5. Full freedom to developers what to return >> >> * Simply because a hard limitation is not required. Let people clone the >> immutable instances if they want too. Nothing bad happens besides >> wasting performance. >> >> I think that this is the simplest and thus best solution. :) >> > > How big of a need is it to allow returning $this instead of $clone, and/o= r > can that be internalized somehow as well? With copy-on-write, is that > really an issue beyond a micro-optimization? > > As an end-user/developer, it's unclear to me how I'd know visually what > scopes an object can be modified in. Basically, in the above example > there's an implicit unlock-on-clone and lock-on-return. There's no clear > indication of that, however, since there's no extra keywords. Is that > sufficiently obvious for developers? I fear not. > > Also, would the above still allow for custom clone() implementations on a= n > immutable object or no? I' not sure off hand which I'd prefer, honestly.= .. > > Another note: This would preclude "externally immutable" objects, eg, one= s > that can compute and internally cache a value but are still effectively > immutable as the outside world sees them. That's probably acceptable sin= ce > the manual way is still available, but I thought it worth calling out. > > I definitely like any of these options better than an explicit user-facin= g > lock/unlock mechanism, as that's begging for abuse, confusion, and > inconsistency. > > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --=20 Silvio Mariji=C4=87 Software Engineer 2e Systems --94eb2c11ce52492856053bdb09c8--