Discussion Thread: DIP 1033--Implicit Conversion of Expressions to Delegates--Community Review Round 1
kinke
noone at nowhere.com
Wed Apr 22 20:42:49 UTC 2020
On Wednesday, 22 April 2020 at 19:08:08 UTC, Petar Kirov
[ZombineDev] wrote:
> We need it in order to avoid API breaking changes. If we
> deprecate `lazy`, without this DIP this code will break:
>
> const result = cache.require(key, calculateValue(key));
I thought these cases could later be simplified by overloading
these few functions, taking either a value directly or a factory
delegate. But after scanning for lazy params in Phobos, it looks
like its use is reasonably restricted to a few places where the
shortcut syntax is indeed nice (enforce, assertThrown,
logging...).
But I still think this DIP would be a step in the wrong
direction; instead, I'd prefer `lazy` to keep designating params
where arguments are eligible for this shortcut syntax / implicit
conversion of expressions to delegates. The problems with lazy
('being an oddity means it is hard to reason about, especially
with the proliferation of parameter attributes') might be solved
by transitioning to requiring a delegate type for lazy
parameters, e.g.:
void write(string);
alias Factory(T) = scope T delegate();
void log(lazy Factory!bool condition, lazy Factory!string msg)
{
if (condition())
write(msg());
}
void foo(int p)
{
log(true, "bla");
log(() => ++p, "blub"); // enable optional delegate syntax too
}
lazy is used very sparingly in druntime/Phobos, and AFAIK, nobody
has requested the proposed implicit conversion for other
expressions so far. So instead of trying out something completely
new and opening the door for abuse and confusion, I think we
should rather focus on the few sprinkled usages of lazy params.
More information about the Digitalmars-d
mailing list