Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Format Strings in Julia's @printf macro #29278

Closed
JimDavidson opened this issue Sep 20, 2018 · 6 comments
Closed

Dynamic Format Strings in Julia's @printf macro #29278

JimDavidson opened this issue Sep 20, 2018 · 6 comments
Labels
domain:io Involving the I/O subsystem: libuv, read, write, etc. kind:feature Indicates new feature / enhancement requests

Comments

@JimDavidson
Copy link

julia> using Printf julia> fmt = "%10d" "%10d" julia> @printf(fmt, 123) ERROR: LoadError: ArgumentError: @printf: first or second argument must be a format string

It would be nice to have dynamic format strings for the @printf macro.

@carstenbauer
Copy link
Member

See also this old issue, #4248

@JimDavidson
Copy link
Author

Yes, I did see that, but hoped something changed in the last 5 years or so. It looks like this behavior is going to be baked into the language.

@lobingera
Copy link

It would be nice to have dynamic format strings for the @printf macro.

Macros are evaluated at compile time, once a printf macro is compiled, the format string is fixed (and yes, that's baked into the language). If you need dynamic formatting goto https://github.com/JuliaIO/Formatting.jl or similar.

@stevengj
Copy link
Member

stevengj commented Sep 20, 2018

@lobingera, in principle, the @printf macro could look at whether the first argument is a literal string (this is known at macro-expansion time since literal strings appear as String nodes in the AST), and if not fall back to slow dynamic code.

This would be backwards-compatible, so in principle it could be supported in 1.x. But it might mean folding something like Formatting.jl into the standard library.

@stevengj stevengj added domain:io Involving the I/O subsystem: libuv, read, write, etc. kind:feature Indicates new feature / enhancement requests labels Sep 20, 2018
@o314
Copy link
Contributor

o314 commented Nov 26, 2019

What is a bit puzzling is that Printf @ stdlib does the macro impl exporting @printf and @sprintf, but Formatting.jl does its function part by exporting printfmt instead of printf, sprintf that have not been reserved by stdlib.
Not a good pattern there

@quinnj
Copy link
Member

quinnj commented Sep 8, 2020

This is now implemented in stdlib Printf: you can create Printf.Format objects by calling Printf.format"..." or Printf.Format(...), then pass a formatting object to Printf.format(::Format, args...) to format arguments according to the format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:io Involving the I/O subsystem: libuv, read, write, etc. kind:feature Indicates new feature / enhancement requests
Projects
None yet
Development

No branches or pull requests

6 participants