Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 11, 2011 Should writef try to statically check for format specifiers? | ||||
---|---|---|---|---|
| ||||
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. |
April 11, 2011 Re: Should writef try to statically check for format specifiers? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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 |
April 11, 2011 Re: Should writef try to statically check for format specifiers? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | %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. |
April 11, 2011 Re: Should writef try to statically check for format specifiers? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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. |
April 11, 2011 Re: Should writef try to statically check for format specifiers? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 |
April 11, 2011 Re: Should writef try to statically check for format specifiers? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | On 4/12/11, Jesse Phillips <jessekphillips+D@gmail.com> wrote:
> Andrej Mitrovic Wrote:
>
>> %s doesn't stand for string in D.
>
> Yes it does.
I was quoting bearophile's "it should error" post, you know that.
|
April 12, 2011 Re: Should writef try to statically check for format specifiers? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | 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
|
April 12, 2011 Re: Should writef try to statically check for format specifiers? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Tue, 12 Apr 2011 01:55:24 +0200, Andrej Mitrovic wrote:
> On 4/12/11, Jesse Phillips <jessekphillips+D@gmail.com> wrote:
>> Andrej Mitrovic Wrote:
>>
>>> %s doesn't stand for string in D.
>>
>> Yes it does.
>
> I was quoting bearophile's "it should error" post, you know that.
I do now. Though it still stands for string ;)
|
April 12, 2011 Re: Should writef try to statically check for format specifiers? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | On 4/12/11, Jesse Phillips <jessekphillips+d@gmail.com> wrote:
> On Tue, 12 Apr 2011 01:55:24 +0200, Andrej Mitrovic wrote:
>
>> On 4/12/11, Jesse Phillips <jessekphillips+D@gmail.com> wrote:
>>> Andrej Mitrovic Wrote:
>>>
>>>> %s doesn't stand for string in D.
>>>
>>> Yes it does.
>>
>> I was quoting bearophile's "it should error" post, you know that.
>
> I do now. Though it still stands for string ;)
>
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. :)
|
April 12, 2011 Re: Should writef try to statically check for format specifiers? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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
|
Copyright © 1999-2021 by the D Language Foundation