Safety/purity and assert/enforce error messages

Meta jared771 at gmail.com
Thu Sep 12 19:47:53 PDT 2013


On Thursday, 12 September 2013 at 22:04:20 UTC, H. S. Teoh wrote:
> Currently, this is not possible, because the format string may 
> be a runtime-computed value. For example:
>
> ...
>

When you don't have a runtime computed format string, though 
(which is
generally the case for my code, at least), it would be extremely 
useful
for a literal format string to be statically verified and 
nothrow. One really nice thing I've seen with the Rust language 
is that they have a fmt macro that statically verifies that the 
types of its arguments match the format string.



> It *is* true, however, that the vast majority of format usage 
> uses a
> static format string. In those cases, it *would* be nice to 
> have a
> nothrow variant of format(). Arguably, we could have this 
> variant of
> format:
>
> 	string staticFormat(string fmt, T...)(T args) { ... }
>
> which takes a compile-time format string. Then it can be 
> statically
> verified to be pure, @safe, nothrow, etc..
>
> This is actually more tricky than it sounds, though. Specifiers 
> like
> "%s" are parsed into Format!char objects, which may be passed 
> to a
> user-defined type's toString method, so the nothrow-ness of
> staticFormat() may change depending on whether the user-defined 
> toString
> throws.

Is it necessary to allow toString to throw? I can't think of any 
reason other than the object being in an invalid state, in which 
case maybe it should throw an Error.



More information about the Digitalmars-d-learn mailing list