Il giorno 23/mar/2014, alle ore 15:00, David Krauss <
pot...@gmail.com> ha scritto:
>
> On 2014-03-23, at 9:34 PM, Nicola Gigante <
nicola....@gmail.com> wrote:
>
>> So you can call it this way:
>>
>> log(lazyexpr("Something wrong"));
>>
>> This is going to work but has some problems:
>>
>> 1. It requires extra syntax at the call site. Some languages that have this features
>> require it anyway, but to me it wastes the effort: why not just write the lambda if I have to use extra syntax anyway?
>
> Based on my experience, I strongly disagree. I would not want lazy evaluation to happen transparently. C++ makes side effects too easy. For example, it's easy to forget a postfix operator on an iterator.
>
> I started making many errors when I tried to make lazy and non-lazy evaluation look similar, and they were hard to debug. It's not a good language direction.
>
If fully transparency is not desirable, one can think about a lightweight enough syntax to "enable" lazyness, just like std::move enables moves.
That's still more convenient than wrapping around a lambda.
>> 2. It relies on a macro.
>
> Meh. Macros should be fixed to work better.
>
I strongly agree.
>> 3. Variables are not always captured in the right way
>
> How would automatic sugar know any better than manual control? I don't see how this is addressed in the OP.
>
> The question of multiple evaluation vs. memoization is also significant, but I'll believe a one-size-fits-all solution when I see it. In the meantime, the language already has the features needed to make various categories of implicit conversion functors:
>
> -- one-shot, by ref-qualifying the conversion function
> -- caching, by storing a std::optional
> -- evaluate-per-use, by default
>
> Each of these can be given a different name but identical usage. I cannot see how static analysis could make such decisions well, though.
>
It doesn't know better than manual control. It knows better than a macro based solution. When the lazy object is
created as a function argument the problem does not arise, and everything can be captured by reference.
When the lazy object is created as a function return value, local variables are captured by value. If the use of
the keyword is made more general (lazy local variables?), more details have to be studied, but I think a general rule
can be found.
Nicola