Re: writefln patterns with mismatching compile-time known number of arguments
On Sunday, 12 April 2015 at 21:34:21 UTC, H. S. Teoh wrote: On Sun, Apr 12, 2015 at 02:33:03PM +, ketmar via Digitalmars-d-learn wrote: On Sun, 12 Apr 2015 14:18:21 +, 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).
Re: writefln patterns with mismatching compile-time known number of arguments
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 and number formatters in my iv.writer. anyway, `std.format` will allocate on %40s, i believe, which is completely unnecessary for `write`, as one can write by arbitrary sized chunks. signature.asc Description: PGP signature
writefln patterns with mismatching compile-time known number of arguments
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?
Re: writefln patterns with mismatching compile-time known number of arguments
On Sun, 12 Apr 2015 14:18:21 +, 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. signature.asc Description: PGP signature
Re: writefln patterns with mismatching compile-time known number of arguments
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.
Re: writefln patterns with mismatching compile-time known number of arguments
On Sun, Apr 12, 2015 at 02:33:03PM +, ketmar via Digitalmars-d-learn wrote: On Sun, 12 Apr 2015 14:18:21 +, 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