Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:89270 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 10798 invoked from network); 17 Nov 2015 16:26:19 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Nov 2015 16:26:19 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.48 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.48 mail-wm0-f48.google.com Received: from [74.125.82.48] ([74.125.82.48:35784] helo=mail-wm0-f48.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 28/E1-30130-A255B465 for ; Tue, 17 Nov 2015 11:26:19 -0500 Received: by wmdw130 with SMTP id w130so162473686wmd.0 for ; Tue, 17 Nov 2015 08:26:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=TuIHN56LKzM1b8x9CrmMQydUfqGdENtpOo5xhcKICo0=; b=x2f75dT0p76R1sWdLWWj9FaoZRY+Nnkxx+/HuDyViGna/wLH805FzTdAgwaM3zKGKw zYT8pgOy0R/8eR56Z817J+WqfesCNCO3zxyL1tiAJJO50bAFLXJtsOJFQeh27Cs+8gOe tgwrkYnj68CzYCA2b2mXUk1dkNe5KwAlRfRNi5+I2HELfLC8u1LCnrRef3xeKPqaYbtC /PgL4FFhXBqpCAKIs2DEZHtEoyKWPMSxC761ZftyoO4K2FQTTH9i5liBNlm/nwOoD9pt 61TmzpDQ+vgmBTppSLq9b0OaFqqJ6oDAGLsnMRFHa4zpgTpnjbcOu0ia9r+rQEetE0jF PpnQ== X-Received: by 10.28.131.11 with SMTP id f11mr3482031wmd.59.1447777575873; Tue, 17 Nov 2015 08:26:15 -0800 (PST) Received: from [192.168.0.157] ([93.188.182.58]) by smtp.googlemail.com with ESMTPSA id q3sm40686299wjr.34.2015.11.17.08.26.14 for (version=TLSv1/SSLv3 cipher=OTHER); Tue, 17 Nov 2015 08:26:15 -0800 (PST) To: internals@lists.php.net References: <564A547C.9060504@garfieldtech.com> <564A629C.9040709@rochette.cc> <564A69C1.5080208@garfieldtech.com> <2D.66.34372.2A78A465@pb1.pair.com> <564B09A4.3050706@gmail.com> <60.01.30130.9A94B465@pb1.pair.com> Message-ID: <564B551A.8080109@gmail.com> Date: Tue, 17 Nov 2015 16:26:02 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: <60.01.30130.9A94B465@pb1.pair.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Immutable modifier From: rowan.collins@gmail.com (Rowan Collins) Andrea Faulds wrote on 17/11/2015 15:37: > Does it? You can write a five-line constructor which does what you need: > > private function __construct(array $properties) { > foreach ($properties as $name => $value) { > $this->{$name} = $value; > } > } > > You can do changes like so: > > public function withName(string $name): self { > return new self(array_merge((array)$this, [ > "name" => $name > ])); > } > > Not much code, no? So, you need to reinvent "clone", in a less efficient, less readable, way; restrict the form of your constructor, and the style of implementation in your withName method; and for what gain exactly? Implementations of this pattern using clone already exist, they are efficient, clear, and maintainable; I'm not sure the promise of run-time checking for mistakes would entice anyone to change them. Would the modification be restricted to the lexical scope of the constructor, or the dynamic scope? i.e. would this be allowed? private function __construct($foo) { $this->setFoo($foo); } If that's allowed, then you basically have a "freeze" action internally anyway, that's called when the constructor returns; it's not a great leap from there to having it exposed to userland to choose when it happens. If that's not allowed, and the modifications have to be in the constructor body itself, the value of the feature is further reduced. That said, I'd be interested in examples of what any other languages do around this area, and how it is used in practice. Regards, -- Rowan Collins [IMSoP]