News for hackers. Stuff that matters.
Re:Implementing a Type-safe printf in Rust by signa11 on Friday August 14, 2020 @04:53AM (#24155513)
imho, with c++'s variadic templates doing the equivalent there is quite trivial.
Re:Implementing a Type-safe printf in Rust by dan-robertson on Friday August 14, 2020 @05:24AM (#24155690)
The issue with templates is that you can’t really type check them at the point of definition. Instead they need to be checked each time the template is used.

This can make compilation slow but it can also make template writing very hard because a bug might only appear when a template is instantiated with certain types.

Re:Implementing a Type-safe printf in Rust by quietbritishjim on Friday August 14, 2020 @07:23AM (#24156503)
The parent comment's point is just that variadic templates would make it easier to do the exact job talked about in this blog post. It sounds like you're objecting but you haven't quite said that outright. Are Rust macros less susceptible to the problem you're describing?
Re:Implementing a Type-safe printf in Rust by monadic2 on Friday August 14, 2020 @12:14PM (#24159799)
Yea, traits make this much simpler to avoid. The problematic type would not match the trait, likely leading to much simpler communication of the error.
Re:Implementing a Type-safe printf in Rust by quietbritishjim on Friday August 14, 2020 @02:25PM (#24161437)
I'm not really convinced. In C++, you could start your hypothetical format function with static_assert(formatter<T>:: exists) or similar and get a very similar error message. (In C++ template terminology, a class like that is also called a trait, funnily enough, but that's just a convention rather than a language feature.) I think you could use C++ concepts to check add part of the function signature, but the functional difference would be very small. And none of these – including Rust traits – help you with compile time checking of format specifiers.
Re:Implementing a Type-safe printf in Rust by signa11 on Friday August 14, 2020 @06:38AM (#24156190)
> The issue with templates is that you can’t really type check them at the point of definition. Instead they need to be checked each time the template is used.

> This can make compilation slow but it can also make template writing very hard because a bug might only appear when a template is instantiated with certain types.

yup. templates are 'compiled' in 2 phases as follows:

- without instantiation at definition time, the code itself is checked for correctness, completely ignoring the template parameters.

- at instantiation time, the code is checked again to ensure that all parts depending on template parameters are valid.

the 2-phase translation breaks canonical notions about compile+link, where declarations of function is sufficient to compile its use...

Re:Implementing a Type-safe printf in Rust by saagarjha on Friday August 14, 2020 @05:42AM (#24155809)
Templates are basically dynamic typing in the compiler, to be honest.
Re:Implementing a Type-safe printf in Rust by dthul on Friday August 14, 2020 @05:12AM (#24155612)
Yes, they make solving such problems quite elegant. The Wikipedia page on variadic templates has a toy printf C++ implementation showcasing that.
Re:Implementing a Type-safe printf in Rust by AshamedCaptain on Friday August 14, 2020 @08:54AM (#24157238)
I kind of agree. The worst part is that most of the problems with C++'s classic "typesafe_printf" implementation come from the fact that there is an extra pass over the formatting string in order to do the actual typecheck. But this rust implementation does not have this problem (no type specifiers in the format string, just placeholders as in {fmt}), and it still looks pretty ugly (and when your reference is a ugly C++ metaprogramming version, that says something).
Re:Implementing a Type-safe printf in Rust by mFixman on Friday August 14, 2020 @08:58AM (#24157285)
Which raises the obvious questions: why didn't C++11 implement template printf along with variadic templates?

`cout <<` is ugly and hard to use for anything but the simplest printing. Type safe printf seems like the obvious use case of variadic templates.

Re:Implementing a Type-safe printf in Rust by signa11 on Friday August 14, 2020 @09:04AM (#24157344)
> Which raises the obvious questions: why didn't C++11 implement template printf along with variadic templates?

https://en.cppreference.com/w/cpp/utility/format and the actual proposal https://www.zverovich.net/2019/07/23/std-format-cpp20.html

Re:Implementing a Type-safe printf in Rust by monadic2 on Friday August 14, 2020 @05:16AM (#24155647)
Yea, but then you have c++ to deal with.