Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81455 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22631 invoked from network); 30 Jan 2015 20:55:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Jan 2015 20:55:07 -0000 Authentication-Results: pb1.pair.com smtp.mail=smalyshev@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=smalyshev@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.50 as permitted sender) X-PHP-List-Original-Sender: smalyshev@gmail.com X-Host-Fingerprint: 209.85.220.50 mail-pa0-f50.google.com Received: from [209.85.220.50] ([209.85.220.50:39411] helo=mail-pa0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 65/44-34022-AAFEBC45 for ; Fri, 30 Jan 2015 15:55:07 -0500 Received: by mail-pa0-f50.google.com with SMTP id rd3so56543426pab.9 for ; Fri, 30 Jan 2015 12:55:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=2Hj/Ku8kK8kXzGvePHG4ytUC/y88pRZPYNNYuITJG6c=; b=1Gdly8qk+auDT5IsWMlOYwemuQl/Mdi6wHi0NiwZ3PYtNPtzKCm0w+LrW/KGyZgL+0 uNlZT2aTP4HWzNprAG02SWkLJdUCKl788aZrm/aA9CCffDFSsbPw8sc8UNLU2jBsc8+0 FNKz69bjfZkXwlINq9Q2fojDjj5CoKpqgd9oFFN5uWxrLSli9on8DTsXQvETJB6oHl9Q luUZoe0Yx8aV0qxi5mo46V6jo/jWVqe9uxB/4WaXEQOcJXG5L1qGp2SEFxiQUrhonRXd TrCoXW4SjXt0cp1BD9s4RErDMyKs3fu82RODEpYWqwxjgfGIOiOe5UvRTthzbqwdjsPr SHKg== X-Received: by 10.70.48.104 with SMTP id k8mr11545689pdn.76.1422651303413; Fri, 30 Jan 2015 12:55:03 -0800 (PST) Received: from Stas-Air.local (108-66-6-48.lightspeed.sntcca.sbcglobal.net. [108.66.6.48]) by mx.google.com with ESMTPSA id kp1sm11640915pbd.54.2015.01.30.12.55.02 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 30 Jan 2015 12:55:02 -0800 (PST) Message-ID: <54CBEFA3.1010908@gmail.com> Date: Fri, 30 Jan 2015 12:54:59 -0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: Alexander Lisachenko , PHP internals list References: In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Immutable variables and objects From: smalyshev@gmail.com (Stanislav Malyshev) Hi! > What I want to discuss is true immutability flag for variables and > parameters. There are a lot of languages that use "final" or "const" > keywords to prevent modification of variables. We can use this approach by > extending language syntax as following: Most of the languages that use "final" and "const" do much more than "prevent modification of variables" - they make these "variables" constants, which behave differently. Also, many of them fail to achieve the actual immutability - "const" declaration just means you can't assign/mutate this name, but says nothing about the object the name points to - it can very well still be mutated by other means. E.g. if you declare "const $foo = new MyObject();" you have no control over what any method called on $foo does. You'd need to introduce const methods and have checks to ensure they are actually not changing the object (which may have serious performance impacts). For all this, I'd like to ask - why? Immutable object are very useful in concurrency applications, since they allow to avoid expensive synchronization costs and make it much easier to reason about system's state (which is arguably impossible to predict with mutable objects in a parallel system). But PHP has no parallelism. So which benefit would that provide? If you want an immutable object, just make all properties protected and provide getters but not setters. This is easily doable without any keywords. Having immutable scalar variable seems a bit useless since the scope of the scalar variable is very small and if you can't track what happens inside a single function you probably should refactor this function, not the language. There might be optimizations available for immutable scalars, but we already have constants for that. > > const $text = "Some message"; // defines an immutable variable that can not > be modified. > $text .= "foo"; // Exception: can not change the immutable variable > > class Test { > public final $value; // final keyword defines immutable property, but > it isn't initialized yet so allows only one assignment, then will be > immutable So each property would have a counter which tracks if it's assigned or not? That would be rather weird - what if the ctor doesn't initialize it? Then random method that tries to assign it would work once but not twice, which would be very weird. > On the engine level this immutable variables and parameters can be > effectively optimized by using IS_TYPE_IMMUTABLE for zval. This can result > in achieving better performance and various JIT-optimizations. I don't see how they can be optimized unless they are actually constants (yours are not, since they can be assigned with arbitrary value in runtime, albeit just once) - and we already have constants. -- Stas Malyshev smalyshev@gmail.com