On Saturday, June 21, 2014 3:02:15 AM UTC-4, Sam L wrote:
>
> If I understand correctly, the @printf and @sprintf macros generate code
> based on the a string literal that is passed as the first argument. You
> cannot pass a variable who's value is a format string to those macros
> because the value would not be known when the macro is called and the code
> is generated.
>
Yea, essentially the macro outputs a block for each % in the format string;
it needs to know the number and kind of blocks to create at macro expansion
time.
But the check is pretty simple; could this be improved? Theoretically, so
long as there's a value to use for the expansion it could work correct? but
it could lead to wrong (misleading) results
How about const variables?
isa(args[1], String) || is_str_expr(args[1]) ||
error("@sprintf: first argument must be a format string")
is_str_expr(ex) =
isa(ex,Expr) && (ex.head == :string || (ex.head == :macrocall &&
isa(ex.args[1],Symbol) &&
endswith(string(ex.args[1]),"str")))