8

How would you implement a function like this:

function foo(a,b...,c)
    println(a,b,c)
end
foo(2,3,3,"last")

=> a = 2 , b = (3,3) , c = "last"

I cannot use something like:

function foo(a,b...) 
    c = b[end]
    println(a,b,c)
end

Because I want to dispatch on c, i.e. I want to have methods:

foo(a,b...,c::Foo)

and

foo(a,b...,c::Bar)

Also I cant have something like this:

foo_wrapper(a,b...) = foo(a,b[1:end-1],b[end])

Because I also want to dispatch on foo in general.

Is this somehow posssible?

ailuj
  • 83
  • 4
  • 3
    No, this is currently not possible. See https://github.com/JuliaLang/julia/issues/42036#issuecomment-909923031 for an explanation of some of the difficulties with that. – Simeon Schaub Dec 18 '21 at 21:30
  • What do you mean by dispatch "on foo in general", exactly? Because you can make multiple methods for `foo(a::Yada, c::Blah, b...)` that dispatches foo based on a and c, then write a single method `foo_wrapper(a,b...) = foo(a, b[end], b[1:end-1])` to reorder arguments for foo. – BatWannaBe Dec 19 '21 at 01:43

2 Answers2

1

you can invert the order and then dispatch on an auxiliary function:

function foo(a,d...)
    c = last(d)
    b = d[begin:end-1]
    return _foo(a,c,b...)
end

function _foo(a,c::Int,b...)
    return c+1
end

function _foo(a,c::Float64,b...)
    return 2*c
end

in the REPL:

julia> foo(1,2,3,4,5,6)
7
julia> foo(1,2,3,4,5,6.0)
12.0

julia> foo(1,2,8.0)
16.0

julia> foo(1,90) #b is of length zero in this case
91
longemen3000
  • 1,273
  • 5
  • 14
0

The only option is to have c as a keyword parameter such as:

function foo(a,b...;c)
    println(a," ",b," ",c)
end

And now you can do:

julia> foo(1,2,3;c="aa")
1 (2, 3) aa
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • Can't dispatch on keyword arguments because positional order is crucial for unambiguous dispatch, and the whole point of keyword arguments is to specify by name instead of position. However, a `foo_wrapper` with a keyword argument `c` can call a method `foo` that takes `c` as a positional argument. However, I don't know off the top of my head if type inference can work its way through` foo_wrapper` to do `foo`'s dispatch on `c`'s type at compile-time. https://docs.julialang.org/en/v1/manual/methods/#Note-on-Optional-and-keyword-Arguments – BatWannaBe Dec 19 '21 at 02:32