5 ms·
What are the downsides of current printf macro implementation vs a variadic fn one?
by methyl 6y ago
What are the downsides of current printf macro implementation vs a variadic fn one?
- The_rationalist 6y agoWell there might be no downsides beyond the need of putting "!", for println. But my point stand for use cases in general of variadic functions where a macro wouldn't be appropriate and anyway macros are not readable.
- naavis 6y agoI think only accepting string literals as the format string is a pretty big downside.
- lmm 6y ago- Possibility of implementation errors - A reader can't really reason about what a macro might be doing, since they're immensely powerful. - Tooling can't necessarily understand macros correctly, for the same reason. E.g. automated refactoring like extracting a repeated expression can't be relied on to work correctly.
- zaarn 6y agoThe thing is though that Rust macro's are hygienic; the macro must generate the syntax tree and cannot mangle outside scope. Most rust tooling I've seen handles and understands printf as a macro easily and tooling is improving to be able to fully introspect a macro.
- methyl 6y agoThese are downsides of macros in general, but I don't see how they are relevant to a built-in macro from stdlib.
- lmm 6y agoThey apply just the same in the standard lib surely? I suppose tooling might be expected to have support for the standard lib, but you're creating a lot more work for tool implementors if there is a large (and presumably evolving) set of "standard" macros. The concern about the reader is definitely still there unless you're expecting every Rust user to memorize which macros are in the standard library.
- perryizgr8 6y agoOne thing I don't like about the macro is that it looks like the passed variable has been borrowed and you can't use it again. But that's not true. You can use it again and again even though you passed by value.
- mqus 6y agoWell, the variables are only borrowed for the short time it takes to print that line. The borrow is terminated as soon as the print is finished. Because the println! macro does not return anything, there is no reason to extend the borrow.
- tsimionescu 6y agoOne big difference is that you can't call it at runtime, e.g. to map it over a list of tuples.