Dispatch on Set{Union}

question

#1

I have a function foo. I want to call

setofbar = Set([Bar()])
setofbaz = Set([Baz()])
foo(setofbar)
foo(setofbaz)

To do so, I declared foo as follows:

function foo(obj::Set{Union{Bar, Baz}})

This gives me a “no method matching” error for the following lines:

foo(setofbar)
foo(setofbaz)

What’s the right way to do dispatch here?


#2

Set{Union{Bar, Baz}} is a set of Bar and Baz, which is not what you pass. You want to accept either a set of Bar, or a set of Baz. That’s Set{T} where T<:Union{Bar, Baz}

EDIT: or Union{Set{Bar}, Set{Baz}}, though it’s not strictly equivalent.


#3

@cjstean I thought I would be able to figure it out based on your solution, but can you give me the concrete function definition for the case outlined?


#4

function foo(obj::Set{T}) where {T <: Union{Bar, Baz}}