Dispatching on arguments after the slurping operator (args...) in julia
Asked Answered
C

2

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?

Claus answered 18/12, 2021 at 20:34 Comment(2)
No, this is currently not possible. See github.com/JuliaLang/julia/issues/42036#issuecomment-909923031 for an explanation of some of the difficulties with that.Bowse
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.Immeasurable
S
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
Sonyasoo answered 19/12, 2021 at 4:33 Comment(0)
C
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
Chamber answered 19/12, 2021 at 1:54 Comment(1)
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. docs.julialang.org/en/v1/manual/methods/…Immeasurable

© 2022 - 2024 — McMap. All rights reserved.