Req #72637 [NEW]: FR: Lazy statement operator
| From: | david dot proweb at gmail dot com | Date: | Wed, 20 Jul 2016 23:29:47 +0000 |
| Subject: | Req #72637 [NEW]: FR: Lazy statement operator | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-202458@lists.php.net to get a copy of this message | ||
From: david dot proweb at gmail dot com
Operating system:
PHP version: Irrelevant
Package: Performance problem
Bug Type: Feature/Change Request
Bug description:FR: Lazy statement operator
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 bug report at https://bugs.php.net/bug.php?id=72637&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72637&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72637&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72637&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=72637&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=72637&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=72637&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=72637&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=72637&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=72637&r=support
Expected behavior: https://bugs.php.net/fix.php?id=72637&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=72637&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=72637&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=72637&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72637&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=72637&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=72637&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=72637&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72637&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=72637&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=72637&r=mysqlcfg