On Fri, 16 May 2014 11:36:44 -0400, Jonathan M Davis via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:

On Thu, 15 May 2014 08:04:59 -0300
Ary Borenszweig via Digitalmars-d-learn
<digitalmars-d-learn@puremagic.com> wrote:

Isn't there a way in D to just expand:

enforce(cond, "failure");

(or something with a similar syntax) to this, at compile-time:

if(!cond) throw new Exception("failure");

I thought D could do this, so enforce should do this instead of using
lazy arguments.

No. enforce is a function, and the only other things that it could be with that syntax would be other callables (e.g. a lambda, delegate, or functor).

I think it *could* optimize properly, and that would be an amazing improvement to the compiler, if someone wants to implement that.

Essentially, you need to be able to inline enforce (not a problem since it's a template), and then deduce that the lazy calls can just be moved to where they are used, in this case, only once.

This would make a logging library even better too.

-Steve

Reply via email to