Req #69738 [Opn]: Declare immutable variables
| From: | cmb@php.net | Date: | Mon, 01 Jun 2015 12:45:59 +0000 |
| Subject: | Req #69738 [Opn]: Declare immutable variables | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-193048@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69738&edit=1
ID: 69738
Updated by: cmb@php.net
Reported by: ugoren at interwise dot co dot il
Summary: Declare immutable variables
Status: Open
Type: Feature/Change Request
Package: Variables related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
At first it should be noted that this syntax (# instead of $) is
not possible, because # starts a line comment (it's an alternative
to //).
| #a=array(1,2);
| #a=array(1,3);//ok
| #a[1]=4;//error: modifying immutable variable
If the second statement is allowed, I wouldn't say that the
variable is immutable, but rather that the value is immutable. It
is, however, already possible to have immutable values by using
immutable objects.
Previous Comments:
------------------------------------------------------------------------
[2015-06-01 11:13:54] chx@php.net
The language specification describes the language as it is. This is a feature request against the
language itself.
------------------------------------------------------------------------
[2015-06-01 11:06:02] ugoren at interwise dot co dot il
Description:
------------
I would like to have an option to declare immutable variables,
for example
$a=1;// is a mutable variable
#b=1;// is an immutable variable
The advantages of immutable variables are numerous:
1. caching function return values
2. parallelism (no shared memory)
3. pass variables by reference
4. Testability, just record a pure function's input and outputs, and you have automatically
generated unit-tests.
Test script:
---------------
//sample code:
#a=array(1,2);
#a=array(1,3);//ok
#a[1]=4;//error: modifying immutable variable
//pure functions:
function #pure_function(#a,#b)
{
return #a+#b;
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69738&edit=1