Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:89231 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 63996 invoked from network); 16 Nov 2015 11:13:30 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Nov 2015 11:13:30 -0000 Authentication-Results: pb1.pair.com smtp.mail=fontanalorenz@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=fontanalorenz@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.51 as permitted sender) X-PHP-List-Original-Sender: fontanalorenz@gmail.com X-Host-Fingerprint: 74.125.82.51 mail-wm0-f51.google.com Received: from [74.125.82.51] ([74.125.82.51:33598] helo=mail-wm0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 36/D1-52418-95AB9465 for ; Mon, 16 Nov 2015 06:13:29 -0500 Received: by wmec201 with SMTP id c201so170509671wme.0 for ; Mon, 16 Nov 2015 03:13:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=3Wx4GK7cdUpWGF1G6VkF4zn8oXqAM3V/NPXexYs2M84=; b=qn4EbrH/g7b+uApGwezZo/zj/vhwUiyyVbj+N6Jk9CyFWJr73K4+oHVenKVRzQJmY/ Ti8Grb17Ch1DcplrUoGSyxRIp/dPn7iHR3u5FN1KzMX6giappiXjMWfEpBQpANArDZwY ZJXD5RZwyuawRs/t18nkU3Z6OcdPA9t2+H1lKuxwZRPBDo0N0hMmQzLTmPS0XBuLUtct TRoVzUp/MA/DNTrnxRE9Q9RMcdHAu4H7ZAjjyJ3ooQf8kt7NoymkbNJ8rCkPVIdlHORp 6MOZ8plT4juubVRMy6nv3cOJGPVc9D7+P5vCx11bMKqk4de9HdmEqYgMIiriQT87EcrN T/0A== MIME-Version: 1.0 X-Received: by 10.28.57.215 with SMTP id g206mr5069921wma.82.1447672405726; Mon, 16 Nov 2015 03:13:25 -0800 (PST) Received: by 10.28.64.138 with HTTP; Mon, 16 Nov 2015 03:13:25 -0800 (PST) In-Reply-To: References: Date: Mon, 16 Nov 2015 12:13:25 +0100 Message-ID: To: Chris Riley Cc: Daniel Persson , PHP internals Content-Type: multipart/alternative; boundary=001a1148e0707e9e0e0524a67f0d Subject: Re: [PHP-DEV] Immutable modifier From: fontanalorenz@gmail.com (Lorenzo Fontana) --001a1148e0707e9e0e0524a67f0d Content-Type: text/plain; charset=UTF-8 2015-11-16 10:40 GMT+01:00 Chris Riley : > > > On 16 November 2015 at 09:33, Lorenzo Fontana > wrote: > >> I really like the concept of immutability, but I think that it should be >> applicable at instance level rather than declaration. >> >> I would also prefer another keyword than immutable. >> >> Final does not make the properties immutable, it makes the class not >> extensible. >> On Nov 16, 2015 10:24, "Daniel Persson" wrote: >> >>> Any differance from the final keyword? >>> >>> http://php.net/manual/en/language.oop5.final.php >>> >>> On Mon, Nov 16, 2015 at 10:15 AM, Chris Riley >>> wrote: >>> >>> > Hi, >>> > >>> > There has been a lot of interest recently (eg psr-7) in immutable >>> data. I'm >>> > considering putting an RFC together to add language support for >>> immutables: >>> > >>> > immutable class Foo { >>> > public $bar; >>> > public function __construct($bar) { >>> > $this->bar = $bar; >>> > } >>> > } >>> > >>> > Immutable on a class declaration makes all (maybe only public?) >>> properties >>> > of the class immutable after construct; assigning to a property would >>> > result in a Fatal error. >>> > >>> > class Foo { >>> > public $bar; >>> > immutable public $baz; >>> > } >>> > >>> > Immutable on a property makes the property immutable once it takes on a >>> > none null value. Attempts to modify the property after this results in >>> a >>> > fatal error. >>> > >>> > Any thoughts? >>> > ~C >>> > >>> >> > What instance level syntax would you propose? > > $foo = new immutable bar(); ? > > or > > immutable $foo = new bar(); > > Gives less flexibility imo and less guarantees of correctness. > > Consider the current user land implementation of immutable classes: > > class Foo { > private $bar; > public function __construct($bar) { > $this->bar = $bar; > } > > public function getBar() { > return $this->bar; > } > } > > Is already done at declaration declaration based immutability is probably > more desirable. > > What keyword would you suggest other than immutable? > > ~C > I would prefer immutable $foo = new bar(); that can also be applied to: immutable $bar = "string"; immutable $baz = 1; immutable $arr = [1, 2, 3]; and not only to classes. The "userland implementation of immutable classes" you are proposing is not immutable at all. First of all one could define another accessor method that can change the state or can bind a closure to $this and change an internal property at runtime. Some possible keywords that "means" immutable to me are: let $bar = "string"; val $bar = "string"; What do you think? --001a1148e0707e9e0e0524a67f0d--