# Multiple dispatch Union

**URL:** https://discourse.julialang.org/t/multiple-dispatch-union/52280
**Category:** New to Julia
**Created:** [December 23, 2020, 3:20pm UTC](https://discourse.julialang.org/t/multiple-dispatch-union/52280 "2020-12-23T15:20:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)
#### Post date: [December 23, 2020, 3:20pm UTC](https://discourse.julialang.org/t/multiple-dispatch-union/52280/1 "2020-12-23T15:20:58Z")

</div>

I have a question and am not even sure how to phrase it correctly. In the end I am wondering if my (toy) implementation can be improved / made more idiomatic to julia. So this is a pure learning question.

**Problem**  
I have a function that takes two parameters as an input both of which are of type `String`. This function is where “all the magic happens”. If either one or both of the two parameters are of type `Array{String,1}` I want to call the above function for each element of the Array(s).

The below code works as intended. However, I am unsure if there is a much more idiomatic “julia” way to do this:

```julia
function to_dataframe(foo::String,bar::String) 
   DataFrame(foo=foo, bar=bar) # Here all the magic happens in my real problem
end;

function to_dataframe(foo::Union{String, Array{String,1}}, bar::Union{String, Array{String,1}})
    foo = isa(foo, String) ? [foo] : foo
    bar = isa(bar, String) ? [bar] : bar
    
    df = DataFrame(foo=String[], bar=String[])
    for a in foo
        for b in bar
            df = vcat(df, to_dataframe(a,b))
        end
    end
    df
end;

```

Thanks for any suggestions!

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [December 23, 2020, 4:16pm UTC](https://discourse.julialang.org/t/multiple-dispatch-union/52280/2 "2020-12-23T16:16:17Z")

</div>

Sincerely, concerning the ‘toy’ example, I think the most Julian thing would be:

```julia
function to_dataframe(foo, bar)
    return DataFrame(foo = foo, bar = bar)
end

```

And ignore all the rest. Yet better, do not even define this function just call DataFrame directly.

The user already has a simple way to mix scalars and lists:

```julia
to_dataframe([string_a], [string_a])
to_dataframe([string_a], array_b)
to_dataframe(array_a, [string_b])
to_dataframe(array_a, array_b)

```

Why complicate the things?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [December 23, 2020, 6:04pm UTC](https://discourse.julialang.org/t/multiple-dispatch-union/52280/3 "2020-12-23T18:04:25Z")

</div>

It only makes sense to have a `Union` as type parameter if the two types have the same behavior. That’s why you see `Union{AbstractString, Symbol}` so much, cause you can sometimes write code that works the same for both.

Strings and vectors are different types with different behaviors entirely. Better use multiple dispatch instead.

---

<div class="post-metadata">

### Author: ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)
#### Post date: [December 23, 2020, 7:15pm UTC](https://discourse.julialang.org/t/multiple-dispatch-union/52280/5 "2020-12-23T19:15:30Z")

</div>

You are partly right because I chose a not so good toy example. Although `to_dataframe(array_a, array_b)` actually yields different results than call DataFrame directly in case array sizes differ.

---

<div class="post-metadata">

### Author: ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)
#### Post date: [December 23, 2020, 7:50pm UTC](https://discourse.julialang.org/t/multiple-dispatch-union/52280/6 "2020-12-23T19:50:04Z")

</div>

I understand that. My follow up question is then:

Should I write four functions?

- function to\_dataframe(foo::String, bar::String)
- function to\_dataframe(foo::Array{String,1}}, bar::String)
- function to\_dataframe(foo::String, bar::Array{String,1})
- function to\_dataframe(foo::Array{String,1}, bar::Array{String,1})

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [December 24, 2020, 12:13am UTC](https://discourse.julialang.org/t/multiple-dispatch-union/52280/7 "2020-12-24T00:13:01Z")

</div>

Yes that sounds good. Do all the work in the `Array`-`Array` version and the other methods just convert.

But this could be an XY problem. Maybe you should re-configure upstream to make sure your inputs are consistent?
