Safety/purity and assert/enforce error messages

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Sep 12 13:24:32 PDT 2013


On Thu, Sep 12, 2013 at 10:17:30PM +0200, 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()
> ... ? :-)

Using assumeWontThrow, as defined in my previous post, you can write
this:

	auto myFunc(int x) nothrow {
		// Look, ma! This line doesn't violate nothrow!
		assert(isValid(x), assumeWontThrow!format(
			"Invalid args (%s), dude!", x));

		... // do work here
	}

:-)


T

-- 
"Uhh, I'm still not here." -- KD, while "away" on ICQ.


More information about the Digitalmars-d-learn mailing list