Safety/purity and assert/enforce error messages

Jonathan M Davis jmdavisProg at gmx.com
Thu Sep 12 13:23:47 PDT 2013


On Thursday, September 12, 2013 22:17:30 Joseph Rushton Wakeling wrote:
> On 12/09/13 22:07, Jonathan M Davis wrote:
> > format can't be nothrow, because it throws when you screw up the format
> > specifiers. You have to wrap it in a try-catch block and assert(0) in the
> > catch block if you want to put it in a nothrow function. std.datetime
> > does this in at least a few places.
> 
> The annoyance of this is that it means that any otherwise @safe pure nothrow
> function that has an assert() in it can be blocked from being nothrow if it
> needs a formatted string for the assert failure message.
> 
> Note that this restriction still applies even if the code is being compiled
> with -release and the assertion therefore stripped out.
> 
> Or am I missing a trick as to how to deal with assert() and enforce() ... ?
> :-)

You can put the try-catch in a version(assert) block to get around that 
problem, but it is true that it's not exactly ideal. However, there really 
isn't any way around that with a function that takes a format string unless 
you want it to ignore bad arguments. You'd need a function like text which 
just appends all of its arguments together in order to get around that 
problem.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list