www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - writefln patterns with mismatching compile-time known number of

reply "JR" <zorael gmail.com> writes:
I was chatting with a friend and showed him how printf("%s") 
printed random memory in C, whereas writefln("%s") in D threw an 
Exception upon execution. It's probably not a completely fair 
comparison but that's a different topic.

I admit to being confused as to why it passed compilation at all 
in the first place. Surely the "%s" literal is just as known at 
compilation as an enum would be.

Is there any button in D that could be leveraged to statically 
assert that the number of arguments precisely match the number of 
format specifiers, iff the pattern is static/enum/literal?

I asked in #d and had it pointed out that this could be solved 
via an overload of writefln that took the pattern as a template 
instantiation argument, to which I conceed. The main con would 
naturally be that you end up with template bloat. You could also 
have tooling (dfix/dscanner) handle it for you.

But the compiler has all the pieces of information needed to see 
it's wrong, doesn't it?

How much of this kind of thing is evaluated during the CTFE 
passes?
Apr 12 2015
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 12 Apr 2015 14:18:21 +0000, JR wrote:

 But the compiler has all the pieces of information needed to see it's
 wrong, doesn't it?
no, it doesn't. compiler doesn't know about `std.format.format` and it's=20 special abilities. while it is possible to add such checks to the=20 compiler, it will introduce interdependency between compiler and phobos,=20 which is not desirable. writing CTFE `writef` version is possible without template bloat in=20 binary, but it will take more memory in compile time and slows=20 compilation. there were talks about having `writef!"fmt"(args)` in phobos,=20 but nobody took that task yet.=
Apr 12 2015
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Sun, Apr 12, 2015 at 02:33:03PM +0000, ketmar via Digitalmars-d-learn wrote:
 On Sun, 12 Apr 2015 14:18:21 +0000, JR wrote:
 
 But the compiler has all the pieces of information needed to see
 it's wrong, doesn't it?
no, it doesn't. compiler doesn't know about `std.format.format` and it's special abilities. while it is possible to add such checks to the compiler, it will introduce interdependency between compiler and phobos, which is not desirable. writing CTFE `writef` version is possible without template bloat in binary, but it will take more memory in compile time and slows compilation. there were talks about having `writef!"fmt"(args)` in phobos, but nobody took that task yet.
It's not hard to write a CTFE version of writef/writeln/etc., that takes the format argument at compile-time, since std.format itself is already CTFE-able. All it takes is for somebody to step up to the plate and implement it. T -- Genius may have its limitations, but stupidity is not thus handicapped. -- Elbert Hubbard
Apr 12 2015
next sibling parent "biozic" <dransic gmail.com> writes:
On Sunday, 12 April 2015 at 21:34:21 UTC, H. S. Teoh wrote:
 On Sun, Apr 12, 2015 at 02:33:03PM +0000, ketmar via 
 Digitalmars-d-learn wrote:
 On Sun, 12 Apr 2015 14:18:21 +0000, JR wrote:
 
 But the compiler has all the pieces of information needed to 
 see
 it's wrong, doesn't it?
no, it doesn't. compiler doesn't know about `std.format.format` and it's special abilities. while it is possible to add such checks to the compiler, it will introduce interdependency between compiler and phobos, which is not desirable. writing CTFE `writef` version is possible without template bloat in binary, but it will take more memory in compile time and slows compilation. there were talks about having `writef!"fmt"(args)` in phobos, but nobody took that task yet.
It's not hard to write a CTFE version of writef/writeln/etc., that takes the format argument at compile-time, since std.format itself is already CTFE-able.
Except for floating-point values (at least).
Apr 13 2015
prev sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 12 Apr 2015 14:31:40 -0700, H. S. Teoh via Digitalmars-d-learn
wrote:

 It's not hard to write a CTFE version of writef/writeln/etc., that takes
 the format argument at compile-time, since std.format itself is already
 CTFE-able.
i didn't know that (didn't checked, actually), so i rewrote simple string=20 and number formatters in my iv.writer. anyway, `std.format` will allocate=20 on "%40s", i believe, which is completely unnecessary for `write`, as one=20 can write by arbitrary sized chunks.=
Apr 13 2015
prev sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Sunday, 12 April 2015 at 14:18:23 UTC, JR wrote:
 I was chatting with a friend and showed him how printf("%s") 
 printed random memory in C
I'm pretty sure modern C compilers will warn about something as obviously wrong as this.
Apr 12 2015