writefln patterns with mismatching compile-time known number of arguments

JR via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 12 07:18:21 PDT 2015


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?


More information about the Digitalmars-d-learn mailing list