www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - writef %d of string

reply bearophile <bearophileHUGS lycos.com> writes:
This code, that a sane language/language implementation refuses at
compile-time, runs:


import std.stdio;
void main() {
    writefln("%d", "hello");
}


And it outputs:
['h', 'e', 'l', 'l', 'o']

Is this good/expected/acceptable/buggy?

Bye,
bearophile
Oct 12 2011
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
That's a sweet trick right there. I get different results but I guess
this is due to new std.format changes:

[104, 101, 108, 108, 111]
Oct 12 2011
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
On 12/10/2011 23:41, bearophile wrote:
 This code, that a sane language/language implementation refuses at
compile-time, runs:

It's perfectly legal code, so the best a compiler can correctly do is give a warning. Some C(++) compilers understand printf and will warn if the format string doesn't match the arguments, but even this is rare AIUI. To enforce well-formed format strings at compile time would require it to be made a language builtin. Or maybe template metaprogramming can do it.
 import std.stdio;
 void main() {
      writefln("%d", "hello");
 }


 And it outputs:
 ['h', 'e', 'l', 'l', 'o']

 Is this good/expected/acceptable/buggy?

It certainly should either throw an exception or generate more sane output such as Andrej is getting. What DMD version are you using? Stewart.
Dec 01 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Stewart Gordon:

It's perfectly legal code,<

If that code is legal, then in my opinion it's the rules of the language that are wrong and in need to be changed :-)
Some C(++) compilers understand printf and will warn if the format string
doesn't match the arguments, but even this is rare AIUI.<

I don't know what 'AIUI' means. And GCC (and Clang too) do it, so it's not a rare thing.
To enforce well-formed format strings at compile time would require it to be
made a language builtin.<

Or just a built-in rule, as in GCC.
Or maybe template metaprogramming can do it.<

Of course. It's not hard to create something like this: writelnt!"%d %f"(53, 1.55); But I think you need a D compiler able to remove unneeded template instantiations from the binary, to avoid bloat.
It certainly should either throw an exception or generate more sane output such
as Andrej is getting.  What DMD version are you using?<

I am using 2.057GitHead on 32 bit Windows. I don't know if this Phobos bug is in bugzilla. Bye, bearophile
Dec 01 2011
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
On 02/12/2011 03:19, bearophile wrote:
 Stewart Gordon:

 It's perfectly legal code,

If that code is legal, then in my opinion it's the rules of the language that are wrong and in need to be changed :-)

You mean all programming languages should support CTFE for argument validation? What if the format string isn't even known at compile time in the first place?
 Some C(++) compilers understand printf and will warn if the format string
doesn't
 match the arguments, but even this is rare AIUI.

I don't know what 'AIUI' means.

As I understand it. http://www.acronymfinder.com/ is your friend.
 And GCC (and Clang too) do it, so it's not a rare thing.

I meant rare among the world's various C compilers. Are you going by usage statistics?
 To enforce well-formed format strings at compile time would require it to be
made a
 language builtin.

Or just a built-in rule, as in GCC.

What do you mean by a "built-in rule" exactly?
 Or maybe template metaprogramming can do it.

Of course. It's not hard to create something like this: writelnt!"%d %f"(53, 1.55); But I think you need a D compiler able to remove unneeded template instantiations from the binary, to avoid bloat.

Maybe there's a way that the template instances can be very small, delegating most of the work to non-templated functions. Stewart.
Dec 04 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Stewart Gordon:

 You mean all programming languages should support CTFE for argument validation?

This is not necessary, and it's not sufficient...
 What if the format string isn't even known at compile time in the first place?

In this case the format string validation is done at run-time. It's opportunistic static typing. And it's better than leaving all the tests at run-time. In a dynamically typed language I accept format strings to be verified at run-time. In a compiled language I want some of advantages of static typing, where possible.
 http://www.acronymfinder.com/ is your friend.

Or even better, reduce the usage of uncommon acronyms in normal communications :-)
 I meant rare among the world's various C compilers.  Are you going by usage
statistics?

In the end D wants to be better than C and C compilers.
 What do you mean by a "built-in rule" exactly?

In GCC there is a hard-coded routine that tests (or tries to) the correctness of format strings known at compile-time.
 Maybe there's a way that the template instances can be very small, delegating
most of the 
 work to non-templated functions.

This is easy to do, just call the same runtime function after the compile-time test done in the template :-) But DMD probably has to learn to better remove unnecessary template instances from the resulting binary. Walter has discussed this topic a bit several times. Bye, bearophile
Dec 04 2011