6 ms·
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. T
by dan-robertson 6y ago
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.
- saagarjha 6y agoTemplates are basically dynamic typing in the compiler, to be honest.
- signa11 6y ago> 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...
- quietbritishjim 6y agoThe 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?
- monadic2 6y agoYea, traits make this much simpler to avoid. The problematic type would not match the trait, likely leading to much simpler communication of the error.
- quietbritishjim 6y agoI'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.