Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97379 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 38894 invoked from network); 12 Dec 2016 19:36:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Dec 2016 19:36:53 -0000 X-Host-Fingerprint: 94.0.142.252 unknown Received: from [94.0.142.252] ([94.0.142.252:14603] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 19/8F-27518-25CFE485 for ; Mon, 12 Dec 2016 14:36:52 -0500 Message-ID: <19.8F.27518.25CFE485@pb1.pair.com> To: internals@lists.php.net References: Date: Mon, 12 Dec 2016 19:36:47 +0000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:43.0) Gecko/20100101 Firefox/43.0 SeaMonkey/2.40 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Posted-By: 94.0.142.252 Subject: Re: [PHP-DEV][RFC][DISCUSSION] - Immutable classes and properties From: ajf@ajf.me (Andrea Faulds) Hi Silvio, Silvio Marijić wrote: >Discussion is open for following rfc https://wiki.php.net/rfc/immutability The restriction that immutable properties may only hold immutable values seems potentially overzealous. Consider that not all immutable classes will actually be marked as such. Any code written in, say, PHP 7.2 if it included this feature, would not be able to make an immutable property if it pointed to an immutable object made before PHP 7.2 which couldn't use this feature. Another consideration is that your existing restrictions (only objects of immutable classes and resources) are not restrictive enough to achieve the desired end of enforcing deep immutability. Arrays can contain object values, as well as array values which might themselves contain values that are objects. They also can contain references. In order to enforce that immutable properties do not point to anything mutable, you would have to search any given array for mutable values, including sub-arrays. You would need to make sure you check for recusion here, lest assigning to an immutable property potentially crash the interpreter. This could be a significant runtime cost, it's at least an O(n) operation. I think that, realistically, trying to enforce immutability of values in an immutable class is not worth it, and makes the feature less useful. Consider that some popular applications of immutability ultimately contain a reference to something mutable: PSR-7's immutable HTTP message objects point to mutable streams, and Docker's immutable containers point to mutable volumes. Thanks. -- Andrea Faulds https://ajf.me/