digitalmars.D.learn - Should writef try to statically check for format specifiers?
- Andrej Mitrovic (11/11) Apr 11 2011 There is a bug here:
- bearophile (8/9) Apr 11 2011 GCC is often able to catch such bugs, but Walter has recently closed my ...
- Andrej Mitrovic (4/4) Apr 11 2011 %s doesn't stand for string in D.
- Jesse Phillips (3/7) Apr 11 2011 I think it should do what it use to do which print the extra data at the...
- Andrej Mitrovic (2/5) Apr 11 2011 I was quoting bearophile's "it should error" post, you know that.
- Jesse Phillips (2/10) Apr 11 2011 I do now. Though it still stands for string ;)
- Andrej Mitrovic (3/13) Apr 11 2011 It still kicks printf's butt for not having to worry about writing all
- Jesse Phillips (4/10) Apr 11 2011 Two complaints:
- bearophile (4/8) Apr 11 2011 Sorry, you are right, I am silly.
- Steven Schveighoffer (12/27) Apr 12 2011 Why would runtime checks be out of the question? You are already parsin...
- Andrej Mitrovic (5/5) Apr 12 2011 I thought templated functions can be called without a bang if an
- bearophile (4/8) Apr 12 2011 I think there is some confusion here. Each function call is done fully a...
- Andrej Mitrovic (2/4) Apr 12 2011 So isn't the string literal a candidate in this case?
- Peter Alexander (3/4) Apr 26 2011 debug doRuntimeCheck();
There is a bug here: import std.stdio; void main() { int index; writef("The index is", index); } Actually I found this bug in some example code: http://d.puremagic.com/issues/show_bug.cgi?id=5836 writef is missing a format specifier. But it still accepts this code. Is it possible for writef to statically check whether there's a format specifier, assuming there's multiple arguments and the first argument is a string literal? I realize runtime checks would be out of the question, but I'm looking for compile-time checks when it's possible to do so.
Apr 11 2011
Andrej Mitrovic:I realize runtime checks would be out of the question, but I'm looking for compile-time checks when it's possible to do so.GCC is often able to catch such bugs, but Walter has recently closed my enhancement request about this :-( http://d.puremagic.com/issues/show_bug.cgi?id=4458 So a solution is to build a fwrite/fwriteln that uses a compile-time string for formatting, that performs compile-time tests using the more efficient CTFE of DMD 2.053: int foo, bar; fwriteln!"%d %s"(foo, bar); // compile-time error: bar isn't a string! Bye, bearophile
Apr 11 2011
%s doesn't stand for string in D. My issue is that the arguments don't get printed out due to missing specifiers. That is clearly a bug. But maybe it's not a huge bug since it's just printing that is involved.
Apr 11 2011
Andrej Mitrovic Wrote:%s doesn't stand for string in D.Yes it does.My issue is that the arguments don't get printed out due to missing specifiers. That is clearly a bug. But maybe it's not a huge bug since it's just printing that is involved.I think it should do what it use to do which print the extra data at the end, or it should throw an exception.
Apr 11 2011
On 4/12/11, Jesse Phillips <jessekphillips+D gmail.com> wrote:Andrej Mitrovic Wrote:I was quoting bearophile's "it should error" post, you know that.%s doesn't stand for string in D.Yes it does.
Apr 11 2011
On Tue, 12 Apr 2011 01:55:24 +0200, Andrej Mitrovic wrote:On 4/12/11, Jesse Phillips <jessekphillips+D gmail.com> wrote:I do now. Though it still stands for string ;)Andrej Mitrovic Wrote:I was quoting bearophile's "it should error" post, you know that.%s doesn't stand for string in D.Yes it does.
Apr 11 2011
On 4/12/11, Jesse Phillips <jessekphillips+d gmail.com> wrote:On Tue, 12 Apr 2011 01:55:24 +0200, Andrej Mitrovic wrote:It still kicks printf's butt for not having to worry about writing all the right specifiers. Just put in %s and life's easy. :)On 4/12/11, Jesse Phillips <jessekphillips+D gmail.com> wrote:I do now. Though it still stands for string ;)Andrej Mitrovic Wrote:I was quoting bearophile's "it should error" post, you know that.%s doesn't stand for string in D.Yes it does.
Apr 11 2011
bearophile Wrote:So a solution is to build a fwrite/fwriteln that uses a compile-time string for formatting, that performs compile-time tests using the more efficient CTFE of DMD 2.053: int foo, bar; fwriteln!"%d %s"(foo, bar); // compile-time error: bar isn't a string!Two complaints: fprintf? A leading f is for file. %s means format as a string not the type is a string.Bye, bearophile
Apr 11 2011
Jesse Phillips:Two complaints: fprintf? A leading f is for file. %s means format as a string not the type is a string.Sorry, you are right, I am silly. Bye, bearophile
Apr 11 2011
On Mon, 11 Apr 2011 16:35:39 -0400, Andrej Mitrovic <none none.none> wrote:There is a bug here: import std.stdio; void main() { int index; writef("The index is", index); } Actually I found this bug in some example code: http://d.puremagic.com/issues/show_bug.cgi?id=5836 writef is missing a format specifier. But it still accepts this code. Is it possible for writef to statically check whether there's a format specifier, assuming there's multiple arguments and the first argument is a string literal? I realize runtime checks would be out of the question, but I'm looking for compile-time checks when it's possible to do so.Why would runtime checks be out of the question? You are already parsing the string at runtime, why can't it say "hey, I processed the whole format string, but I have these arguments left over"? Would be a simple if statement... A compile-time check would be nice, but you'd have to pass it as a compile-time argument (i.e. a template parameter), which would be not-so-nice. What would be nice is if the compiler could check when you give it a string literal, and resort to runtime checks when it was a variable. I don't think that's possible, however. -Steve
Apr 12 2011
I thought templated functions can be called without a bang if an argument can be deduced to be available at compile time. I know I've read about this somewhere, either TDPL or the docs. So I thought that writef checks the string literal at compile time, not runtime. Template shenanigans..
Apr 12 2011
Andrej Mitrovic:I thought templated functions can be called without a bang if an argument can be deduced to be available at compile time. I know I've read about this somewhere, either TDPL or the docs. So I thought that writef checks the string literal at compile time, not runtime.I think there is some confusion here. Each function call is done fully at compile time or fully at run time, there is no partial compilation. Template arguments always require the bang, and they are always taken at compile-time. Template functions may not require a bang+types if argument types can be inferred from the given arguments. Bye, bearophile
Apr 12 2011
On 4/12/11, bearophile <bearophileHUGS lycos.com> wrote:Template functions may not require a bang+types if argument types can be inferred from the given arguments.So isn't the string literal a candidate in this case?
Apr 12 2011
On 11/04/11 9:35 PM, Andrej Mitrovic wrote:I realize runtime checks would be out of the question...debug doRuntimeCheck(); :-)
Apr 26 2011