Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:95996 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37212 invoked from network); 14 Sep 2016 17:08:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Sep 2016 17:08:55 -0000 Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 212.232.28.122 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 212.232.28.122 mx201.easyname.com Received: from [212.232.28.122] ([212.232.28.122:56760] helo=mx203.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5B/D6-21040-32489D75 for ; Wed, 14 Sep 2016 13:08:55 -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 1bkDfy-0007mJ-Jc; Wed, 14 Sep 2016 17:08:48 +0000 Reply-To: internals@lists.php.net References: <56acc1d9-f424-a460-59be-3a9a1a74b198@fleshgrinder.com> <95832b08-ee80-18c1-a3da-202eed51903e@fleshgrinder.com> <2c115733-8fe4-5230-a9ec-9d5f2cc2b810@garfieldtech.com> <06ce2daf-4112-3464-b6d6-47f1ece05303@garfieldtech.com> <26794926-90fc-d102-91c9-948ffe9a6665@texthtml.net> To: Mathieu Rochette , internals@lists.php.net Message-ID: <900846da-74f2-8056-f580-1b74c458e380@fleshgrinder.com> Date: Wed, 14 Sep 2016 19:08:31 +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: <26794926-90fc-d102-91c9-948ffe9a6665@texthtml.net> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="hMTApPGXOOMIThAlXvKTt1An2pj0h6wtb" Subject: Re: [PHP-DEV] RFC - Immutable classes From: php@fleshgrinder.com (Fleshgrinder) --hMTApPGXOOMIThAlXvKTt1An2pj0h6wtb Content-Type: multipart/mixed; boundary="4f7gSuQuUPRfu36Pmd6q0QtuuJTHTM4o4"; protected-headers="v1" From: Fleshgrinder Reply-To: internals@lists.php.net To: Mathieu Rochette , internals@lists.php.net Message-ID: <900846da-74f2-8056-f580-1b74c458e380@fleshgrinder.com> Subject: Re: [PHP-DEV] RFC - Immutable classes References: <83fa661e-2d3d-6548-a506-fb969be31c0e@garfieldtech.com> <56acc1d9-f424-a460-59be-3a9a1a74b198@fleshgrinder.com> <95832b08-ee80-18c1-a3da-202eed51903e@fleshgrinder.com> <2c115733-8fe4-5230-a9ec-9d5f2cc2b810@garfieldtech.com> <06ce2daf-4112-3464-b6d6-47f1ece05303@garfieldtech.com> <26794926-90fc-d102-91c9-948ffe9a6665@texthtml.net> In-Reply-To: <26794926-90fc-d102-91c9-948ffe9a6665@texthtml.net> --4f7gSuQuUPRfu36Pmd6q0QtuuJTHTM4o4 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 9/13/2016 11:38 PM, Mathieu Rochette wrote: > I agree that blocking clone seems unnecessary. I also don't see why it > is useful to have "clone" methods. Why not let "clone $this" produce a > copy that is open to modification within the scope it's cloned in ? or,= > why would you limit yourself to one clone per method call ? maybe there= > is something I'm missing but I don't get why introducing a new method > annotation is useful. it looks like an implementation details (using > clone instead of new) of the method is leaking >=20 > class Natural { > private $n; > public function __construct(int $n) {assert($n >=3D 0); $this->n =3D $= n;} > public function nextTen() { > for ($i =3D 0, $c =3D $this; $i < 10; $i++, $c =3D clone $c;) { > $c->n++; > yield $c; > } > } > } Why would you want to clone here? final immutable class NaturalNumber { private $n; public function __construct(int $n) { assert($n >=3D 0); $this->n =3D $n; } public function nextTen($n) { for ($i =3D $this->n; $i < 10; ++$i) { yield new static($i); } } } That being said, the whole example is kind of weird. What kind of functionality is that? Why is a value object creating multiple instances for itself? Single responsibility seems to be broken because some kind of collection should take care of that anyways. final immutable class NaturalNumber { private $n; public function __construct(int $n) {/*...*/} public clone function add(int $n) { $this->n +=3D $n; return $this; } } final class NaturalNumberCollection { public function nextTen(NaturalNumber $start) { // This cast does not work (yet) ... for ($i =3D (int) $start; $i < 10; ++$i) { yield $start->add($i); } } } Same result but we just separated things nicely. I think the clone modifier would help to create better code and keep it easy to understand.= On 9/13/2016 11:38 PM, Mathieu Rochette wrote: > again, why mark method as clone method in an interface, if the interfac= e > is already marked as immutable isn't it enough to specify the return > type "static" ? Because it is unclear if you want to mutate in this context or return a new instance altogether. --=20 Richard "Fleshgrinder" Fussenegger --4f7gSuQuUPRfu36Pmd6q0QtuuJTHTM4o4-- --hMTApPGXOOMIThAlXvKTt1An2pj0h6wtb 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 iQIcBAEBCAAGBQJX2YQYAAoJEOKkKcqFPVVro0cQALrIHvKJNWZfvZ2e5RiKt2E3 +F8ainA6PJTJyNzHM2Ws89L/SvTY/UYpBfxUPkccUe7ekvSaWBEF3lHHmnzKaJbA Uz3CkAGpESL2S1JAh3zy09qAuLGg0L2jHRtf3+/ZeUDaeooUnt6MsbJhiHqI8SZj zBxaxG08NZivk/r6dBwRWo5Uqi0CdnN8rhxio6zUp9uQ4XDqmtYkXYYi92XJFbUk AfUqQRByRZGAAW9g8+xor1nGKR/DXaCnjcGOeA91j+3nas2Ps4oZ2CXsL6mBr/Fx 6MNRK8nlotO3Nn4ArEPxUAW65FeCUyiYyvEVBIMhMs4RQakGJ4fVzOhh6jlBWmCh UsdyU8jc+ceMUduVcYYhbRJWhle4ZTcMbQtI3tMEf8a6P98uN230N2xCsQ0KNlEp vuYdvpH5KLWZX6opUB3hO0zEipdWMvj9ud8lf7ESgjRKsygo2QMBdGj5XpW1cPDa 4p74mkSMjJQVbMdDvI3qff9zQnTJoMpSq1e1SM3JquXfF1pH8ezJ8oUldmDMgfzN ddygx47MiOPiFR+dgO10aQzdrh7oSuAO+l4jFtPJIgWdYDUrlSwCn+UFQGFIMHSQ D3gi/kVwcZcEUaFOLA3FYgR3/X0zCeKr9BLCnzmYYM2ceR6SQf1+pdxyhmyRIcZu +EFVEKu3HsDHeOJmIEbq =hy1V -----END PGP SIGNATURE----- --hMTApPGXOOMIThAlXvKTt1An2pj0h6wtb--