Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:94665 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 70354 invoked from network); 24 Jul 2016 05:15:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Jul 2016 05:15:06 -0000 Authentication-Results: pb1.pair.com header.from=david.proweb@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=david.proweb@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.179 as permitted sender) X-PHP-List-Original-Sender: david.proweb@gmail.com X-Host-Fingerprint: 209.85.220.179 mail-qk0-f179.google.com Received: from [209.85.220.179] ([209.85.220.179:35599] helo=mail-qk0-f179.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 01/C9-05797-6DE44975 for ; Sun, 24 Jul 2016 01:15:03 -0400 Received: by mail-qk0-f179.google.com with SMTP id s63so132642500qkb.2 for ; Sat, 23 Jul 2016 22:15:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=LJEs3AZdbgq2ChH4JpYIQ0KyyHfrAOIXuSaQvhWRm3Y=; b=jqxe/MFrfx18BAT4hVR+IZ+sk31rd12CbvzASGzOtlvy1u2fxFVDolLdfHcF3fUj06 5NIaDmmiaJPKCR2gSffFVDJnDAbHb8qRmecB1gRz3gqcCXyYsJ7nvWztBAAuZTJP62MA Dv9X3q9XpXYYYFGRvC7RxyLEMAmbNsRfW4ag9LZd3RW0nH4nWxHbSbPPyKTZV9FvUzl3 HlhVX7ZH1emOXAwQTy/EYjaT0ZelidQ6nzPyYa41XDLQEbABp1r2MaPBfqu4IGi8gyvV HuMss8aVfakxy5GGYA/6oVKlEfYVCp5p8H3J1CsSxyYa2EqSH/mKzNZJ7hokY7S7+79r xiTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=LJEs3AZdbgq2ChH4JpYIQ0KyyHfrAOIXuSaQvhWRm3Y=; b=kA/9RB+KSkFJCVXWdVkh6uk6u0fmz+i2R8PLiZ9tINPMQ3nj1Th8qVrhIq6CYA5kor 8jDirO4tiIVc49S7SYXuVoiipHJqEWmo8/wBEQqg2CwM0/1Bln6Jn6rtkod1i1CDUMFJ gL3jRXTzAch3ms9VbKrnrk/g2KofHRAVBBzZ8zbRVEAoTPcdXKbMbUgUUVuMtRCkaY2Z S/vzpOZQAvgeNQ598ivy0qIeXEO9drXyXo64/7qkvjhb3M/RA28BF5VnEapDBjYPCgSF eH02BeMyzX4Wr9fNaeYuBafIZdQ7P8zaSzknGjPhRwccltqr4a/Izy0a7rtZjs+XXH+8 BczA== X-Gm-Message-State: AEkoout+BFYSpp9P+5OeVlZ+O+NhgE3Ifl8yUVUBiOYf1xNRaTjYMKVuJyREvCkKSX9xzKcMJYJXVXOA4gl1jg== X-Received: by 10.55.64.140 with SMTP id n134mr13981455qka.201.1469337299723; Sat, 23 Jul 2016 22:14:59 -0700 (PDT) MIME-Version: 1.0 Received: by 10.55.45.67 with HTTP; Sat, 23 Jul 2016 22:14:40 -0700 (PDT) In-Reply-To: References: Date: Sun, 24 Jul 2016 02:14:40 -0300 Message-ID: To: Stanislav Malyshev , Dan Ackroyd Cc: PHP Internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] RFC: lazy statements From: david.proweb@gmail.com (David Rodrigues) Dan Ackroyd wrote: > 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. On really it seems a very good feature focused on method call. Maybe it can be done in same idea, by freezing the first result of method. But I think hard do that with args. Maybe something like that: http://pastebin.com/YKjEYeYF Note: I know that is better an IoC, understand just as example. Dan Ackroyd wrote: > 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. Now back to topic, this circular dependency too occur with functions. Basically if it happen, should throw a infinite recursion error. (Currently I guess that PHP don't do that, but xdebug does) In all case, $a should not depends of $b, when $b depends $a. In this case, should have a bootstrap system, that turn it independent. Example: class Foo { public $a, $b; public __construct () { $c = lazy { return generalDependency(); } $this->a = lazy use ($c) { return 1 + $c; }; $this->b = lazy use ($c) { return 1 + $c; }; } } Dan Ackroyd wrote: > Because the 'memoization' of functions is less magic, it is much less > prone to this circular dependency problem. I guess that memoization is another topic, but still very interesting, as cited above. So maybe it can be discussed apart of this email, if is reasonable. Stanislav Malyshev wrote: > > This Feature Request is about the implementation of lazy statements. > > > > Basically it should implements a 'lazy' keyword (or similar) and > > structured like an anonymous function, except that it doesn't accepts > > arguments, but accept the use() feature. > > > > $var = lazy { return 1; } > > Is this just keystroke saving for function() or is this supposed to do > something additional? It's similar to function (), but should be applied to variables or properties only. But exists an important difference between you use lazy vs function (). $var1 = function () { sleep(5); return 1; } $var2 = lazy { sleep(5); return 1; } - $var1 should be called like $var1(); - If I call $var1() five times, it'll execute this structure over 25 seconds; - If I try to write over $var1(), possible I should get an error trying to write over int (1); - $var2 should be called like $var2 directly; - If I call $var2 five times, it'll execute once the structure, custing 5 seconds only; - If I try to write over $var2 it'll, in fact, write over $var2 processed structure, that stores int (1);