-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
@printf() constant format string is not allowed #4248
Comments
Our "constants" are still run-time values – they are write-once rather than compile-time constants like in C. |
As a hack until julia gets real support for runtime format strings, this works: julia> const fmt="%4d%4d%4d\n"
"%4d%4d%4d\n"
julia> @eval @printf($fmt,1,2,3)
1 2 3 |
You could also hack in a quoted string and avoid the |
@pao , how's that? |
Poor man's function to do the above: julia> printf(fmt::String,args...) = @eval @printf($fmt,$(args...))
# methods for generic function printf
printf(fmt::String,args...) at none:1
julia> printf(fmt, 1,2,3)
1 2 3 |
I'll have to check with a Julia console. Let me get back to you. |
...but the idea is to splice in an expression containing a literal string. |
If you define |
Hence the description as a "poor man's printf". What do you suggest? AFAIK, right now there's no other way to handle format strings generated on the fly. (I was looking for this myself yesterday...) |
You can use julia> const fmt = "%4d%4d%4d\n"
"%4d%4d%4d\n"
julia> @eval f(x,y,z) = @printf($fmt,x,y,z)
# methods for generic function f
f(x,y,z) at none:1
julia> f(1,2,3)
1 2 3 The function |
@StefanKarpinski's approach is basically what I was thinking. |
Thanks for all your answers! |
Julia's In the meantime, you can handle all scenarios with our current |
I would actually suggest rewording this issue or creating a separate issue requesting runtime string formatting. In particular, other than I was wanting runtime formatting for the
I can't seem to find |
Stefan, thanks for summarizing all these options. A nice thing about |
Right, we have |
Let's create a new issue for programmatic formatting. I'm not entirely sure what needs to go in there so I'll let someone else open it. |
I agree that it's quite powerful. I'm probably going to use something like it |
The following code is invalid:
Is there a reason why a constant format string is not allowed ?
The text was updated successfully, but these errors were encountered: