Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:95657 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 92484 invoked from network); 5 Sep 2016 16:37:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Sep 2016 16:37:23 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 77.244.243.84 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 77.244.243.84 mx103.easyname.com Received: from [77.244.243.84] ([77.244.243.84:34049] helo=mx202.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 01/B3-45301-04F9DC75 for ; Mon, 05 Sep 2016 12:37:21 -0400 Received: from cable-81-173-132-21.netcologne.de ([81.173.132.21] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1bgwtW-0004cm-SJ for internals@lists.php.net; Mon, 05 Sep 2016 16:37:15 +0000 Reply-To: internals@lists.php.net References: <0e71d28e-1d64-5372-b58d-e54c7afae3b8@fleshgrinder.com> <642a6e78-90ea-cbf0-ec1c-376c24e568c5@fleshgrinder.com> To: internals@lists.php.net Message-ID: <0800a5ca-3d14-c541-1a1a-2574ec802b8c@fleshgrinder.com> Date: Mon, 5 Sep 2016 18:37:01 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="3bL01QHPPKrn0IdiCbF36FJhN2SVlReE8" Subject: Re: [PHP-DEV] RFC - Immutable classes From: php@fleshgrinder.com (Fleshgrinder) --3bL01QHPPKrn0IdiCbF36FJhN2SVlReE8 Content-Type: multipart/mixed; boundary="C9TnmKglEV3mSIJ6epDR3Q8pICvfeNw2k"; protected-headers="v1" From: Fleshgrinder Reply-To: internals@lists.php.net To: internals@lists.php.net Message-ID: <0800a5ca-3d14-c541-1a1a-2574ec802b8c@fleshgrinder.com> Subject: Re: [PHP-DEV] RFC - Immutable classes 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> In-Reply-To: --C9TnmKglEV3mSIJ6epDR3Q8pICvfeNw2k Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 9/5/2016 4:21 AM, Micha=C5=82 Brzuchalski wrote: > You may begin with modifications on RFC eg. in a gist so we can all > discuss about it before putting on RFC, is that okay to you? Absolutely, that's how I thought about approaching it too because otherwise we might create a mess in the Wiki. 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 b= egins > 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: >=20 > 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 create= d clone > return $this; > } > } >=20 > $oneHundret =3D new Money(100, $eur); > $twoHundret =3D $oneHundret->amount(200); >=20 > 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. 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. :) --=20 Richard "Fleshgrinder" Fussenegger --C9TnmKglEV3mSIJ6epDR3Q8pICvfeNw2k-- --3bL01QHPPKrn0IdiCbF36FJhN2SVlReE8 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJXzZ81AAoJEOKkKcqFPVVrjXQQAL1K9cnhYDgWfq6EN87Bq8ys g1VeVlX2CkqnNV8qTy5GtNsI59BHt3uZJiNgEr0KVdBE7b0+LjGY4p9F0R2jSBcl 20QG+txg7uLts0fVCi/t0QkABEQt24Bck3hchxgl1ZwM4gGxvgLMKr/QId7kGk9+ Td3nBt9+rAc1a1qGr3Zzo4jAY5MVVvQbccIoMC0GoYAMQYyoLjrGFQeNzVMtrPbV Pb2gJiTpbpxbOsu77B53QS7o5nU63DbfBso0lL+o8XIlLuKxwRMtn0kq/x51PCbn TdjyVqUPNtTnGNc2g4Wd2vB8o8+N8Ih9wPa8jKinP0h52U48xB+SNyKQf/ibLDB/ v4jRgS1i9fRIBcm6mApIkvF9onIg3J3XjfF8PM/6vmStTog/OG2kUGTJOoMxKpcA ec91s2sjUycXpHxuYdKTtDBUkkrYpvAt0SDvrjpFlisq2ge82ku1sOk6kytbehaK xFoKXFedt0ofhENODwJVA0Mf8f20P1qW6KeYTPXHwWaHuwlrvl3BVoqb8/r9KbSj FXUxe0LS6a+De7g0BchvpHjYCX8W9wCqmL9chKU8WoLA7IMbKFYj82kc5kkadI2n jdrtXS/NmVyYRG4vWrLjPvsCs+B04uSg5y7+Dla0p+OjH8i35H5zd7QMFr8f/WBK 5e+6O6Qi8xDKffLi1XrF =b4QV -----END PGP SIGNATURE----- --3bL01QHPPKrn0IdiCbF36FJhN2SVlReE8--