Printf with variable format string

Imho, printf should not be a macro in the first place. In the end, its just a small performance optimization precompiling the format string, i.e., it doesn’t buy much as compared to its underlying functions:

julia> @macroexpand @printf "%d" 123
:((Printf).format(stdout, Printf.Format{Base.CodeUnits{UInt8, String}, Tuple{Printf.Spec{Val{'d'}}}}(UInt8[0x25, 0x64], UnitRange{Int64}[1:0, 3:2], (%.0d,)), 123))

# So basically the same as
julia> Printf.format(stdout, Printf.format"%d", 123)
123
# i.e. with the format_str macro precompiling the Format
 
# Why not just define ...
julia> printf(fmt::Printf.Format, args...) = Printf.format(stdout, fmt, args...)

julia> printf(io::IO, fmt::Printf.Format, args...) = Printf.format(io, fmt, args...)

julia> macro fmt_str(s) Printf.Format(s) end

# and be happy
julia> printf(fmt"%d", 123)
123

Am I missing something here, i.e., also Rust decided on format being a macro?

3 Likes