Varargs function chaining

Let foo take a variable number of string arguments and pass them to bar. foo and bar have the same signatures.

julia> function foo(strings::String...)
           bar(strings)
           println("Hello, foo")
       end
foo (generic function with 1 method)

julia> function bar(strings::String...)
           println("Hello, bar")
       end
bar (generic function with 1 method)

I expected this to work and it does without the varargs elipsis. But as written above, I get as follows–

julia> foo("abc")
ERROR: MethodError: no method matching bar(::Tuple{String})
Closest candidates are:
  bar(::String...) at REPL[2]:2
Stacktrace:
 [1] foo(::String, ::Vararg{String,N} where N) at ./REPL[1]:2

julia> foo("abc", "def")
ERROR: MethodError: no method matching bar(::Tuple{String,String})
Closest candidates are:
  bar(::String...) at REPL[2]:2
Stacktrace:
 [1] foo(::String, ::Vararg{String,N} where N) at ./REPL[1]:2

I need an iterable collection of foo’s arguments in bar. Can someone explain why this is not working and what the proper way to accomplish this is? TIA.

You need

bar(strings...)
1 Like

Thanks. That did it with one twist.

julia> function foo(strings::String...)
           println("foo: ", typeof(strings))
           bar(strings)
       end
foo (generic function with 1 method)

julia> function bar(strings...)
           println("bar: ", typeof(strings))
           println("bar: ", typeof(strings[1]))
       end
bar (generic function with 1 method)julia> foo("abc")

foo: Tuple{String}
bar: Tuple{Tuple{String}}
bar: Tuple{String}

julia> foo("abc", "def")
foo: Tuple{String,String}
bar: Tuple{Tuple{String,String}}
bar: Tuple{String,String}

So strings within bar is not the same type as strings within foo. But strings[1] within bar is. Thanks again.

The point was to define

julia> function foo(strings::String...)
           println("foo: ", typeof(strings))
           bar(strings...)
       end

which will circumvent that additional level of Tuple.

4 Likes

I see it now. Thanks again.