On Fri, 16 May 2014 11:51:31 -0400 Steven Schveighoffer via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
> 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. Sure, the compiler could be improved to optimize enforce such that enforce(cond, "failure"); becomes if(!cond) throw new Exception("failure"); And I think that that's what needs to be done. However, what I understood the question to be was whether there was a construct in the language that we could use where enforce(cond, "failure"); would be automatically converted to if(!cond) throw new Exception("failure"); without the compiler having to do optimizations to make it happen. The closest to that would be mixins, but they can't do it with the same syntax. You'd be required to write something more complex to use mixins to do the job. But I think that the correct solution is to improve the compiler with regards to lazy. The fact that lazy is so slow is a serious problem, and enforce is just one manifestation of it (albeit the worst because of how often it's used). - Jonathan M Davis