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). Mixins are the only constructs that can completely replace themselves with another construct. So, I suppose that you could have a function called enforce that returned a string and be able to do something like mixin(enforce("cond", `"failure"`)); and you could probably do something similar with a template mixin. mixin(enforce!(cond, "failure")); might be possible if both of the template parameters were alias parameters. But there's no way to take one piece of code and completely translate it into another piece of code without mixins. To do that probably would have meant using some kind of macros, or that was specifically avoided in D's design. And conceptually, using lazy for enforce is a perfect fit. The problem is with the current implementation of lazy. - Jonathan M Davis