Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:89232 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 66143 invoked from network); 16 Nov 2015 11:31:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Nov 2015 11:31:41 -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.54 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.54 mail-wm0-f54.google.com Received: from [74.125.82.54] ([74.125.82.54:32928] helo=mail-wm0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6A/32-52418-C9EB9465 for ; Mon, 16 Nov 2015 06:31:41 -0500 Received: by wmec201 with SMTP id c201so171272432wme.0 for ; Mon, 16 Nov 2015 03:31:38 -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=qw+vyNN9h/Up055/tmgPdeznAZOqns19/cgmnvVLn4A=; b=hQFAf4+S5FWS3cVTFPjaBz0o+5+grJtsIiVIj8zNIrlp5xUPZkdwXPr+5g1jCMqLM3 Lqb55pXM1Wh15wt7aAtzOAV2ryxHCBjMw8bdsPJAhh/7gXIDrwfXQa4Lr/ltuYaVlm05 aoyQxQ3Y++c/anbTczKGWI+vp0Y0rXIau1cDfFRicjQrszlKJx6FQc/RAOjMHOIt7Laa A9gnH/PgcXEUHMGdLVblkfK4OPieiKHME9T8vgJmz0w9rgfqWkMVX9CCiZtfnDkTWmVd BDb1dM2X9R3gBQ+WY2XS6baMv6RDTNIadJoHC1pyK+6Gnb+xf89pbTE2yrA/Nkd6iDF5 s/MA== X-Received: by 10.28.138.195 with SMTP id m186mr19272126wmd.2.1447673498141; Mon, 16 Nov 2015 03:31:38 -0800 (PST) Received: from [192.168.0.157] ([93.188.182.58]) by smtp.googlemail.com with ESMTPSA id 77sm18145266wml.20.2015.11.16.03.31.37 for (version=TLSv1/SSLv3 cipher=OTHER); Mon, 16 Nov 2015 03:31:37 -0800 (PST) To: internals@lists.php.net References: <5649A951.80707@rochette.cc> Message-ID: <5649BE57.9040604@gmail.com> Date: Mon, 16 Nov 2015 11:30:31 +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: <5649A951.80707@rochette.cc> 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) Mathieu Rochette wrote on 16/11/2015 10:00: > Would it be allowed to assign non immutable types to properties of an > immutable class ? I think this is the trickiest part of trying to enforce immutability: nesting. Consider the following: // Basic properties $my_immutable_object->foo = 42; $my_immutable_object->foo++; // Nested object - implicitly immutable? required to be explicitly immutable? $my_immutable_object->bar->something = 42; $my_immutable_object->bar->setSomething(42); // Array access - no idea how this should work $my_immutable_object->quux[0] = 42; $my_immutable_object->quux[] = 'Hello'; $my_immutable_object->quux['World'][0]['X'] = 'A'; The other problem is that to be useful, certain functions usually need to be able to mutate the state of objects which are officially immutable. For instance, implementations of PSR-7 need a set of copy constructors for the withX methods. You could do this using the suggested rule that immutability applies only to non-null properties, and manually populate every property of an empty instance, but it's much more efficient to "clone $this" and then change the one property that needs to be different. For this particular example, you could write quite a nice implementation by "freezing" the instance once it's initialised: // example plucked from https://github.com/guzzle/psr7/blob/master/src/Request.php#L101 public function withMethod($method) { // The method starts by making a mutable clone of the immutable $this $new = clone $this; $new->method = strtoupper($method); # SUGGESTED ADDITION: new keyword to set an object to be immutable freeze $new; // After this point, nothing should be able to mutate the new object, only clone it return $new; } The freeze operation could perhaps be recursive, or perhaps it could work like "clone", with an "__freeze" method responsible for freezing nested objects as necessary. The ability to freeze an array would also be useful in that case... I realise this is a lot more complex than the original suggestion, but I think something of this level of detail is needed to make it useful. Regards, -- Rowan Collins [IMSoP]