Varargs function chaining


#1

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.


#2

You need

bar(strings...)

#3

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.


#4

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.


#5

I see it now. Thanks again.