Variable arguments with file and line information?

Rob T alanb at ucora.com
Sun Nov 17 11:11:20 PST 2013


On Saturday, 16 November 2013 at 23:55:47 UTC, Jonathan M Davis 
wrote:
[...]
> e.g. force the
> caller to call format when creating the message rather than 
> supporting
> variadic arguments directly in error.
>
> - Jonathan M Davis

OK, how about this implementation?

string msg( S : string, T... )( S a_Msg, T a_Args )
{
     if ( a_Args.length != 0 )
       a_Msg = std.string.format(a_Msg, a_Args);
     return a_Msg;
}

void error(lazy string a_Msg, string file = __FILE__, size_t line 
= __LINE__)
{
     auto v_Msg = a_Msg();
     writeln(.format(v_Msg ~ ". In file %s on line %d.", file, 
line));
}

void main()
{
    error(msg("hallo") );
    error(msg("Hallo %s.", "du da") );

    // I think this looks a whole lot better
    msg("hallo").error;
    msg("Hallo %s.", "du da").error;

}

It works, but still would be convenient if there was another more 
simpler way of getting the job done, also if it were more 
obvious. Once it's done though, it's rather nice I think.

--rt


More information about the Digitalmars-d-learn mailing list