Why can't I generate a function with multiple arguments using Symbol("arg1,arg2")?

Macro interpolation doesn’t work the same as string interpolation. I.e. this is not what you’re getting in that eval

julia> TA = Symbol("a,b");

julia> "f($TA)"
"f(a,b)"  # you're not getting this. At least not the way you think...

You’re getting something more like

f(Symbol("a, b")) = a + b

So it’s a one argument function, with input argument Symbol("a, b"). What’s incredibly bizarre about that input argument name, is that it prints as though a and b are different things! That’s what’s throwing you off:

julia> methods(f)
# 1 method for generic function "f":
[1] f(a,b) in Main at REPL[5]:1

julia> f(1,1)
ERROR: MethodError: no method matching f(::Int64, ::Int64)
Closest candidates are:
  f(::Any) at REPL[5]:1
#...
julia> f(1)
ERROR: UndefVarError: a not defined
#...

Note that in the first case, there is no such function at all. The method printout is just messed up because your symbol is really strange. In the second case, there is such a function (one argument) but it doesn’t know what to do with a and b, since they are not the inputs.

2 Likes