Safety/purity and assert/enforce error messages

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Sep 12 11:49:19 PDT 2013


On Thu, Sep 12, 2013 at 08:13:37PM +0200, Joseph Rushton Wakeling wrote:
> Hi all,
> 
> Suppose that I want to insert some variable values into an assert
> error message.  The easy way to do this is something like:
> 
>     assert(someCondition, text("x = ", x, ", cannot blah blah blah"));
> 
> However, the use of text() prevents me from applying @safe and other
> such attributes to the function containing this assert, even though
> text() will only be called in the event of an assertion failure.
> 
> Is there any reliable way to include variable values in the error
> message that doesn't interfere with @safe, pure, etc.?  Using
> to!string has a similar problem.
[...]

In phobos git HEAD, std.format has been made pure @safe nothrow (and
CTFE-able), so you should be able to write your assert as:

	assert(condition, format("x = %s, blahblah", x));

Don't think it will work on 2.063.2, though, since it was a relatively
recent change. I suspect this will only work starting from 2.064.


T

-- 
MASM = Mana Ada Sistem, Man!


More information about the Digitalmars-d-learn mailing list