4 ms·
Are you saying the compiler itself does the checking? That is a pity. Compiler magic for a checking a fixed specific format system (I don't know about D, but th
by quietbritishjim 6y ago
Are you saying the compiler itself does the checking? That is a pity. Compiler magic for a checking a fixed specific format system (I don't know about D, but that's what you're describing in C printf) is significantly worse the compile-time checking being written in the language itself.
What if you, as library author (not compiler writer!), think of a much better formatting system? Or something that's maybe not better in general, but better for an unusual situation you're deploying in? Or you want to write something that's different to a formatting system but uses similar tricks? If you don't have language facilities to do this yourself then you're stuffed.
As well the original post showing it's possible in Rust, this is also possible in C++. For example, the C++ fmt [1] library allows compile-time time checking of the number of slots in format strings against the number of arguments, and even that the format specifications (.03d or whatever) match the passed in types. And it's not reliant on the compiler doing its own magic checking outside of the language.
[1] https://github.com/fmtlib/fmt https://github.com/fmtlib/fmt
- rightbyte 6y agoCome on ... printf is a monstrousity and I understand why you would put hard coded checks in the compiler.
- quietbritishjim 6y agoSure, I'm not disputing that. In fact I think that proves my point. There are languages (C++, Rust) where you can write your own, safe formatting library just using the language. It's a world away from printf. Compared to that, parent comment says that in D the compiler has to have checks for formatting built in to it and compares it to printf. Yuk!
- WalterBright 6y agoAllow me to clarify. D does have writef() which provides a fully typesafe print formatting routine. It is unknown to the compiler (i.e. no magic). https://dlang.org/phobos/std_stdio.html#.writef https://dlang.org/phobos/std_stdio.html#.writef D can also call C's printf, and checking that is magical indeed. It exactly follows the C99 Standard for printf. A major feature of D is being able to call C functions directly, and printf is one of them. (D doesn't recognize printf specifically, it has to have a special attribute for it `pragma(printf)` which the compiler recognizes. This is so the checking can be applied to user-defined functions that use conforming printf-style format strings.) https://dlang.org/spec/pragma.html#printf https://dlang.org/spec/pragma.html#printf And the code to do it: https://github.com/dlang/dmd/blob/master/src/dmd/chkformat.d https://github.com/dlang/dmd/blob/master/src/dmd/chkformat.d
- quietbritishjim 6y agoOK, thanks for clarifying. Reading back your original comment, I can see that its literal wording does say that. I was confused by you leading with printf handling, which is not really relevant to the original article (certainly not as much as writef()), so I inferred you were only mentioning it as context to writef() working similarly. Your use of the word "simply" also threw me off, it sounded like you thought compiler magic was a better solution that a language-level one. I now see you were just making an unrelated point first. And having a specific compiler feature just for printf (and co.) is totally reasonable given how widespread it. Your first link answers what would've been a follow up question: OK, writef() is a variadic template implemented with just normal language features, but does it do compile-time checking of the format? The docs says that it does: > fmt: The format string. When passed as a compile-time argument, the string will be statically checked against the argument types passed.
- pests 6y agoWalter Bright is the creator of the D language so this is the best place to ask these questions.
- MaxBarraclough 6y agoedit I see I'm too late, Walter already answered this. I'll leave this here anyway. I don't think D's writefln is getting any special treatment from the compiler. It seems to use D's compile-time features to perform the check. https://github.com/dlang/phobos/blob/v2.093.0/std/stdio.d#L4095 https://github.com/dlang/phobos/blob/v2.093.0/std/stdio.d#L4...
- AnIdiotOnTheNet 6y agoZig does it in a similar way, using the compile time type information available to all Zig code, and is in fact implemented in the std library. No magic necessary.
- renox 6y agoBut both fail short to Python/bash.. format strings where you can have "in place" reference to variable which IMHO is much more readable.
- ben-schaaf 6y agoI completely agree that string interpolation is often superior to a format string, but it's important to note that they are not equivalent. String interpolation is generally evaluated in-situ and has full access to the local scope. Format strings on the other hand can be passed around and manipulated (even at compile-time) and can only ever access the data they're formatted with. strftime and strptime are a good example of a format string usage that can't be replaced by string interpolation in python. D also has the capability of implementing string interpolation using mixins - though the resulting code isn't quite as neat.
- CyberDildonics 6y agoFormatting text output is not often a problem in the larger scope of a program, but having performance bottlenecks can be.
- WalterBright 6y agoI did write a proposal for "in place" reference (i.e. string interpolation) but could not reach consensus with the D community for it.