Re: RFC: lazy statements
| From: | Dan Ackroyd | Date: | Sat, 23 Jul 2016 10:06:47 +0000 |
| Subject: | Re: RFC: lazy statements | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-94642@lists.php.net to get a copy of this message | ||
Hi David,
On 21 July 2016 at 16:51, David Rodrigues <david.proweb@gmail.com> wrote:
> This Feature Request is about the implementation of lazy statements.
This looks to solve a problem very similar to the one solved by the
'Memoize' annotation in HHVM -
https://docs.hhvm.com/hack/attributes/special#__memoize
However the memoize functionality seems a lot clearer to me, as it's
less 'magic'; it doesn't make accessing a variable actually call a
function.
Larry Garfield wrote:
>> However, that also means there's an enormous potential for race
>> conditions.
I think s/race conditions/circular dependencies/ probably explain the
problem more clearly.
> I can think about how we can handle this, but I need a real case
> where this problem can exists.
This code has a circular dependency:
class Foo {
public $a, $b;
public __construct() {
$this->a = lazy { return 1 + $this->b; }
$this->b = lazy { return 1 + $this->a; }
}
}
$foo = new Foo;
echo $foo->a;
$foo->a can't be resolved until $foo->b is resolved, and $foo->b can't
be resolved until $foo->a is resolved.
Although that is obvious in a trivial code example, when trying to use
a magic feature like this in complex code, where the circular
dependency involves, say, 10 items, then it become very hard to reason
about the code.
Because the 'memoization' of functions is less magic, it is much less
prone to this circular dependency problem.
cheers
Dan