Safety/purity and assert/enforce error messages

Meta jared771 at gmail.com
Thu Sep 12 14:45:07 PDT 2013


On Thursday, 12 September 2013 at 20:07:40 UTC, Jonathan M Davis 
wrote:
> On Thursday, September 12, 2013 12:42:34 H. S. Teoh wrote:
>> On Thu, Sep 12, 2013 at 09:12:18PM +0200, bearophile wrote:
>> > H. S. Teoh:
>> > >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));
>> > 
>> > With the latest DMD from updated GIT head:
>> > 
>> > 
>> > import std.string: format;
>> > void main() pure nothrow {
>> > 
>> > string x = "hello";
>> > bool condition = true;
>> > assert(condition, format("x = %s, blahblah", x));
>> > 
>> > }
>> > 
>> > 
>> > It gives:
>> > 
>> > test.d(5): Error: 'std.string.format!(char, string).format' 
>> > is not nothrow
>> > test.d(2): Error: function 'D main' is nothrow yet may throw
>> 
>> [...]
>> 
>> Oops. Apparently I wrote nothrow but forgot to test it. I did 
>> test pure
>> and @safe, though, so at least those two should work.
>
> 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.
>
> - Jonathan M Davis

Why don't we have a format function that is statically checked?


More information about the Digitalmars-d-learn mailing list