Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:95633 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 78293 invoked from network); 5 Sep 2016 08:26:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Sep 2016 08:26:17 -0000 Authentication-Results: pb1.pair.com header.from=michal@brzuchalski.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=michal@brzuchalski.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain brzuchalski.com designates 188.165.245.118 as permitted sender) X-PHP-List-Original-Sender: michal@brzuchalski.com X-Host-Fingerprint: 188.165.245.118 ns220893.ip-188-165-245.eu Received: from [188.165.245.118] ([188.165.245.118:60786] helo=poczta.brzuchalski.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 60/3A-45301-62C2DC75 for ; Mon, 05 Sep 2016 04:26:15 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by poczta.brzuchalski.com (Postfix) with ESMTP id 06BE22984235 for ; Mon, 5 Sep 2016 10:26:11 +0200 (CEST) Received: from poczta.brzuchalski.com ([127.0.0.1]) by localhost (poczta.brzuchalski.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id dDpv5eil9C0p for ; Mon, 5 Sep 2016 10:26:05 +0200 (CEST) Received: from mail-qk0-f176.google.com (unknown [209.85.220.176]) by poczta.brzuchalski.com (Postfix) with ESMTPSA id 444DA2984233 for ; Mon, 5 Sep 2016 10:26:05 +0200 (CEST) Received: by mail-qk0-f176.google.com with SMTP id v123so184039298qkh.2 for ; Mon, 05 Sep 2016 01:26:05 -0700 (PDT) X-Gm-Message-State: AE9vXwOwwBe1idGl/xR3xmB96Cy67NnUMqZaXxl9db3UGc72++hCCXyVu+vNAQFokwE1od4o6MaK0BUAWew4ng== X-Received: by 10.55.88.199 with SMTP id m190mr37749526qkb.78.1473063964448; Mon, 05 Sep 2016 01:26:04 -0700 (PDT) MIME-Version: 1.0 Received: by 10.200.56.100 with HTTP; Mon, 5 Sep 2016 01:26:03 -0700 (PDT) In-Reply-To: References: <4f54308a-4a69-2e6b-2ed0-51d4336d1cd4@fleshgrinder.com> <5969d1af-48e5-1376-07fe-9568de538145@texthtml.net> <0e71d28e-1d64-5372-b58d-e54c7afae3b8@fleshgrinder.com> <642a6e78-90ea-cbf0-ec1c-376c24e568c5@fleshgrinder.com> Date: Mon, 5 Sep 2016 10:26:03 +0200 X-Gmail-Original-Message-ID: Message-ID: To: PHP Internals List Content-Type: multipart/alternative; boundary=001a114e507e5522c5053bbe6eb5 Subject: Re: [PHP-DEV] RFC - Immutable classes From: michal@brzuchalski.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=) --001a114e507e5522c5053bbe6eb5 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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 begin= s 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 created cl= one return $this; } } $oneHundret =3D new Money(100, $eur); $twoHundret =3D $oneHundret->amount(200); How about that? 2016-09-05 4:21 GMT+02:00 Micha=C5=82 Brzuchalski : > > > 2016-09-04 22:38 GMT+02:00 Fleshgrinder : > >> On 9/4/2016 2:29 PM, Micha=C5=82 Brzuchalski wrote: >> >> Providing `mutator` | `mut` keyword as method modifier sounds liek a >> good >> >> idea, >> >> althought passing `$clone` parameter as first additional param could >> break >> >> method declaration and would be misleading. >> >> >> >> Assuming mutator method is designed to return mutated clone of >> immutable >> >> object >> >> having `$clone` variable could be handled internally without breaking >> >> method declaration. >> >> >> >> Such variable could be unlocked while in mutator method and locked on >> >> return. >> >> I was thinking about additional check if such mutator returns `$clone= ` >> but >> >> not `$this` >> >> but I don't see the need of it - assuming there is no what to change = in >> >> some >> >> circumstances ther would be also possible to return `$this`. >> >> >> >> The return type declaration `self` could increase readability, but >> should >> >> not be required, >> >> as some developers doesn't already use return types. >> >> >> > >> > It could look like in this gist >> > https://gist.github.com/brzuchal/e7b721e22a19cca42ec0d1f597a23baf >> > >> >> This is exactly how I meant it, yes. I actually prefer it if the >> variable just exist in the context instead of passing it as first >> argument. It's less obvious that it exists but the same could be said >> about $this. >> >> Would it be possible to have this thing just in time instead of >> automatically created in every mutator function? >> >> public mutator function f() { >> if (condition) { >> $clone is created; >> return $clone; >> } >> return $this; >> } >> >> PS: I started overhauling the test cases and error messages and will >> create a PR against your branch soon. I will start with the RFC >> afterwards. Should I simply edit the RFC in the Wiki or should we manage >> this somewhere else? >> >> > You may begin with modifications on RFC eg. in a gist so we can all discu= ss > about it before putting on RFC, is that okay to you? > > >> -- >> Richard "Fleshgrinder" Fussenegger >> >> > > > -- > regards / pozdrawiam, > -- > Micha=C5=82 Brzuchalski > brzuchalski.com > --=20 regards / pozdrawiam, -- Micha=C5=82 Brzuchalski brzuchalski.com --001a114e507e5522c5053bbe6eb5--