assert(expression, error)
Jonathan M Davis
jmdavisProg at gmx.com
Sat Feb 12 07:35:51 PST 2011
On Saturday 12 February 2011 07:05:34 Andrej Mitrovic wrote:
> On 2/12/11, bearophile <bearophileHUGS at lycos.com> wrote:
> > Andrej Mitrovic:
> >> Yeah, enforce is great.
> >
> > Enforce is not disabled in release mode.
>
> Right. That's why I need it in this case, since the library can return
> null at runtime due to user or even (audio) hardware errors.
>
> > Currently a function with enforce
> > inside can't be nothrow, and it can't be inlined.
> >
> > Bye,
> > bearophile
>
> Of course it can't be nothrow? I thought the idea of enforce is to
> throw a custom exception when needed, so how can you expect a function
> which throws an exception to be nothrow?
>
> Btw, is the inline problem just a DMD implementation problem, or does
> enforce have to be fixed for that?
It's because the second argument to enforce is lazy. Whether dmd will ever be
able to reasonably inline functions which take lazy arguments, I don't know
(there's a fair bit that goes on underneath to make lazy happen, so the
resulting code isn't exactly short). However, it has been discussed from time to
time to have a non-lazy version of enforce. That would require either creating
another function (e.g. enforceNonLazy) or making dmd smart enough to be able to
have two versions of a function - one lazy and one non-lazy - and have it use
the non-lazy one when it can (e.g. when it's only given a string and there's
nothing which the resulting delegate would do other than return the string -
making it non-lazy in such a case does nothing).
Regardless, it's why I pretty much never use lazy. I don't think that
if(condition)
throw new Exception("my message");
is onerous at all, and I don't want to worry about the inlining issue. But if
your function isn't likely to be inlined anyway, or for some reason, you just
don't like having the if statement, then enforce is just fine.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list