November 17, 2013 Re: Variable arguments with file and line information? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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
|
November 17, 2013 Re: Variable arguments with file and line information? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rob T | On Sunday, November 17, 2013 20:11:20 Rob T wrote: > 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; > } Why bother with this? Just use format directly. As written, msg is a useless wrapper function for format. > 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)); > } And as error is currently written, making the message lazy is pointless and increases overhead. lazy is only valuable if that parameter might not be used (e.g with the message to enforce). Also, if you're going to use a format string, you should use writefln rather than calling format and passing the result to writeln. It's almost certainly less overhead to call writefln. > 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. It would be nice if we could get __FILE__ and __LINE__ to work as function arguments to variadic functions, but for now, that just isn't possible. - Jonathan M Davis |
November 17, 2013 Re: Variable arguments with file and line information? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | Good points, got it down to this. void error(string a_Msg, string file = __FILE__, size_t line = __LINE__) { writefln( a_Msg ~ ". In file %s on line %d.", file, line ); } int main() { format("hallo").error; format("Hallo %s.", "du da").error; } There should be no more template bloat, and it looks to be about as usable and simple as possible. Do you see any further optimizations that do not increase the usage convenience? --rt |
November 17, 2013 Re: Variable arguments with file and line information? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rob T | On Sunday, November 17, 2013 22:08:38 Rob T wrote:
> Good points, got it down to this.
>
> void error(string a_Msg, string file = __FILE__, size_t line =
> __LINE__)
> {
> writefln( a_Msg ~ ". In file %s on line %d.", file, line );
> }
>
> int main()
> {
> format("hallo").error;
> format("Hallo %s.", "du da").error;
> }
>
> There should be no more template bloat, and it looks to be about as usable and simple as possible.
>
> Do you see any further optimizations that do not increase the usage convenience?
Yeah. Don't use concatenation in your format string:
writefln("%s. In file %s on line %s.", msg, file, line);
Other than that, it looks fine. Personally, I'd change it to
writefln("%s(%s): %s", file, line, msg);
but that's personal preference.
- Jonathan M Davis
|
November 17, 2013 Re: Variable arguments with file and line information? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Sunday, 17 November 2013 at 21:29:03 UTC, Jonathan M Davis wrote:
>
> Yeah. Don't use concatenation in your format string:
>
Right, that concat was bothering me too. Tks for the input.
--rt
|
Copyright © 1999-2021 by the D Language Foundation