Some @nogc text conversion in Phobos?

Dmitry Olshansky via Digitalmars-d digitalmars-d at puremagic.com
Thu May 29 11:29:45 PDT 2014


29-May-2014 03:52, Dicebot пишет:
> On Wednesday, 28 May 2014 at 23:27:34 UTC, bearophile wrote:
>> This is currently accepted code:
[....]
>
> IMHO something like this is better:
>
> class MyException : Exception
> {
>      private char[20] index_buff; // should be enough for size_t.max

Why not just save 'size_t' right here, and do formattedWrite in the 
toString? Anyway I think this is what exceptions should do - save the 
state and only ever format message in toString.

>
>      this(string msg, size_t index, string file = __FILE__, size_t line
> = __LINE__, Throwable next = null)
>      {
>          super(msg, file, line, next);
>      // in-place conversion of size_t to char[]
>      }
>
>      void toString(void delegate(const(char)[]) sink) const
>      {
>          sink(msg);
>          sink(" (at index ");
>          sink(index_buff);
>          sink(")");
>      }
> }
>
> Benefits: no arbitrary message length limit, no string construction
> unless toString is actually called.

Yup.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list