Use a string with interpolations as function argument and interpolate later

Hello, I can use this code easily to produce a tuple (or array) of strings:

julia> Tuple("a$(a)_b$(b)" for (a,b) ∈ Iterators.product(1:2, 3:4))
("a1_b3", "a2_b3", "a1_b4", "a2_b4")

However I cannot make a function from this like:

julia> f(s) = Tuple(s for (a,b) ∈ Iterators.product(1:2, 3:4)); 
julia> f("a$(a)_b$(b)")
ERROR: LoadError: UndefVarError: a not defined

Is there a (complicated) way to define such a function via expressions and so on?

I really would love to have this but on my own I was not able to find a way of doing it…

Thanks.

1 Like

One way:

julia> f(s) = Tuple(s(a,b) for (a,b) ∈ Iterators.product(1:2, 3:4));

julia> f((a,b) -> "a$(a)_b$(b)")
("a1_b3", "a2_b3", "a1_b4", "a2_b4")
3 Likes

Yeah, that is great. Thanks…