On Thu, 15 May 2014 05:53:45 +0000
monarch_dodra via Digitalmars-d-learn
<digitalmars-d-learn@puremagic.com> wrote:

> As a workaround, I'm sure we could specialize enforce without
> lazy for built-in types?

No. I don't think that that would work. The problem is that you'd have to be
able to overload between stuff like "error message" and
format("error message: %s", foo), because you don't want the first one to be
lazy, whereas you do want the second one to be lazy.

> BTW: Why *is* enforce lazy again? I don't really see it. I makes
> more sense for things like "collectException" I guess, but I
> don't see it for enforce.

It's lazy so that the second argument isn't evaluated. That might not be
obvious in the example

enforce(cond, "failure");

but if you have

enforce(cond, format("failure: %s", foo));

or

enforce(cond, new BarException("blah"));

then it would definitely be costing you something if the second parameter
weren't lazy - and in theory, the cost of it not being lazy could be
arbitrarily large, because you can pass it arbitrarily complex expressions
just so long as their result is the correct type. Now, given how slow lazy is,
maybe it would actually be faster to just have it be strict instead of lazy,
but in theory, it's saving you something. And if lazy were actually properly
efficient, then it would definitely be saving you something. Regardless, using
an explicit if statement is definitely faster, because then you don't have the
lazy parameter to worry about _or_ the potentially expensive expression, since
the expression would only be encountered if the condition failed.

- Jonathan M Davis

Reply via email to