Safety/purity and assert/enforce error messages

2013-09-12 Thread Joseph Rushton Wakeling
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

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread H. S. Teoh
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 f

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread Jonathan M Davis
On Thursday, September 12, 2013 13:21:26 H. S. Teoh wrote: > On Thu, Sep 12, 2013 at 04:07:24PM -0400, 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

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread H. S. Teoh
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

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread bearophile
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 nothr

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread Jonathan M Davis
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(condi

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread H. S. Teoh
On Thu, Sep 12, 2013 at 04:07:24PM -0400, 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-ab

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread Jonathan M Davis
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

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread Joseph Rushton Wakeling
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.

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread Joseph Rushton Wakeling
On 12/09/13 20:49, H. S. Teoh wrote: 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 r

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread H. S. Teoh
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")); > > How

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread Joseph Rushton Wakeling
On 12/09/13 22:23, Jonathan M Davis wrote: 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 argum

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread H. S. Teoh
On Thu, Sep 12, 2013 at 11:37:10PM +0200, Joseph Rushton Wakeling wrote: > On 12/09/13 22:23, Jonathan M Davis wrote: > >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

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread Meta
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-ab

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread H. S. Teoh
On Thu, Sep 12, 2013 at 11:45:07PM +0200, Meta wrote: > On Thursday, 12 September 2013 at 20:07:40 UTC, 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 bl

Re: Safety/purity and assert/enforce error messages

2013-09-12 Thread Meta
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