Sent from my Alcatel Onetouch Idol 3 (4.7) On Sep 14, 2016 7:08 PM, Fleshgrinder <p...@fleshgrinder.com> wrote: > > 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 > > > > class Natural { > > private $n; > > public function __construct(int $n) {assert($n >= 0); $this->n = $n;} > > public function nextTen() { > > for ($i = 0, $c = $this; $i < 10; $i++, $c = 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 >= 0); > $this->n = $n; > } > > public function nextTen($n) { > for ($i = $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.
yeah the example is not that great, I'll usually want to clone to avoid calling a constructor with to many parameters (or a constructor doing too many things not needed here) > > final immutable class NaturalNumber { > > private $n; > > public function __construct(int $n) {/*...*/} > > public clone function add(int $n) { > $this->n += $n; > > return $this; > } > > } > > final class NaturalNumberCollection { > > public function nextTen(NaturalNumber $start) { > // This cast does not work (yet) ... > for ($i = (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 interface > > 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. what's the difference between the two for an immutable class? > > -- > Richard "Fleshgrinder" Fussenegger >