Req #72637 [Opn->Sus]: FR: Lazy statement operator
| From: | requinix@php.net | Date: | Thu, 21 Jul 2016 02:33:03 +0000 |
| Subject: | Req #72637 [Opn->Sus]: FR: Lazy statement operator | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-202462@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72637&edit=1
ID: 72637
Updated by: requinix@php.net
Reported by: david dot proweb at gmail dot com
Summary: FR: Lazy statement operator
-Status: Open
+Status: Suspended
Type: Feature/Change Request
-Package: Performance problem
+Package: Scripting Engine problem
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
This is the kind of feature request that needs a discussion on the mailing list and an RFC. https://wiki.php.net/rfc/howto
Previous Comments:
------------------------------------------------------------------------
[2016-07-20 23:29:43] david dot proweb at gmail dot com
Description:
------------
Should be great if PHP could have a lazy statement operator.
Imagines that a lazy statement operator is like an anonymous function that could be called as a
variable or property (it basically translate this function into the variable value at runtime when
it is readed or writed the first time). It is very useful because you don't need to test all
the time what is the type of this variable is before you run that.
Simulation simulation using current PHP:
// Defining the lazy property.
$someClass->someProperty = function () {
return 1;
};
// In some moment, ONLY when I read this property:
// It basically will "compile" the statement and set to own property or variable.
if ($someClass->someProperty instanceof LazyStatement) {
$someClass->someProperty = $someClass->someProperty();
}
// Now I print it normally.
echo $someClass->someProperty.
Implementation by using the lazy operator:
$someClass->someProperty = lazy { return 1; }
echo $someClass->someProperty;
It could be used with some codes where you don't need use it all the time, only for specific
cases that depends basically of user demand. Imagine that a framework, for instance, could define
some lazy properties that are only calculated when user request it, and not all the time or by using
magic methods.
Simulation simulation using current PHP:
/**
* @property-read int $value
*/
class Test {
// Note: should be declared as private.
// And should be declared as a property on doc.
private $value;
public function __get($key) {
if ($key === 'value') {
if ($this->value === null) {
$this->value = calculatePrimeNumberNth(10000);
}
return $this->value;
}
}
}
$test = new Test;
echo $test->value;
Implementation by using the lazy operator:
class Test {
public $value = lazy { return calculatePrimeNumberNth(10000); }
}
$test = new Test;
echo $test->value;
It should works to be writed too. Basically, on write the lazy implementation should be executed and
setted on object-variable before to run the writer code, so things like that are valids:
$array = lazy { return [ 1, 2, 3 ]; }
$array[] = 4;
$array now is [ 1, 2, 3, 4 ]
And should be possible works with use() like anonymous functions, it should be useful to use local
scoped informations.
$number = 123;
$text = lazy use ($number) { return (string) $number; }
$number = 456;
echo $text;
Should prints 456, instead of 123 that was overwroted.
It should allow complexes statements too, but should return something (else, will be returned null,
what make no sense):
$number = lazy {
$sum = random_int(0, 100);
return $sum;
};
echo $number;
It could PHP IDEs to check some conditions where variables are readed optionally and tells to use
lazy operator, or then check conditions where lazy operator are not need:
Where could use lazy operator:
$number = calculatePrimeNumberNth(10000);
if (complexTest()) {
return $number;
}
if (anotherComplexTest()) {
doSomethingBefore();
return $number;
}
return 0;
IDE Warning: you could use lazy operator on line 0 because $number could be not used in all
the time.
Where is not useful to use lazy operator:
$number = lazy { return 123; }
echo $number;
IDE Warning: lazy operator at line 0 could be removed because $number is always used on
code.
Simulation simulation using current PHP:
$number = function () { return calculatePrimeNumberNth(10000); };
// Same code after.
Implementation by using the lazy operator:
$number = lazy { return calculatePrimeNumberNth(10000); };
// Same code after.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72637&edit=1