# Trying to understand parametric args in functions

**URL:** <https://discourse.julialang.org/t/trying-to-understand-parametric-args-in-functions/99052>\
**Category:** New to Julia\
**Created:** [May 18, 2023, 11:34am UTC](https://discourse.julialang.org/t/trying-to-understand-parametric-args-in-functions/99052 "2023-05-18T11:34:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [May 18, 2023, 11:34am UTC](https://discourse.julialang.org/t/trying-to-understand-parametric-args-in-functions/99052/1 "2023-05-18T11:34:56Z")

</div>

I have looked at the docs for parametric args in functions and cannot understand how to apply it to what I want to program.

I have a function `function ga(s::String,g::T)` where `::T` can be a `::String` or a `::Array{Num}` and have an `if` statement in `ga` that does on thing for `::String` and another for `::Array{Num}`. Because there is a lot of common code for each case I don’t want to define two functions (in the `g::String` case I have code to convert `g::String` to `g::Array{Num}`) -

`function ga(s::String,g::String)`

`function ga(s::String,g::Array{Num})`

Is there a way I can do this with parametric arguments (multiple dispatch)? Thank you for any help you can give me.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [May 18, 2023, 11:39am UTC](https://discourse.julialang.org/t/trying-to-understand-parametric-args-in-functions/99052/2 "2023-05-18T11:39:11Z")

</div>

> Because there is a lot of common code for each case I don’t want to define two functions

You don’t have to, you can dispatch somewhere in the function, too.

```julia
function ga(s::String, g)
   g = make_array(g)
   # other code
end

make_array(g::Array{Num}) = g
make_array(g::String) = [1, 2, 3]

```

In terms of compilation latency, it’s best to do all conversions towards the unified format first, then dispatch to the “real” function doing all the work. Then that one will not be compiled again and again.

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [May 18, 2023, 12:29pm UTC](https://discourse.julialang.org/t/trying-to-understand-parametric-args-in-functions/99052/3 "2023-05-18T12:29:12Z")

</div>

Thank you. I thought of something similar after writing my question -

````julia
function ga(a::String,g::String)
    g = make_array(g)
    return ga(a::String,g::Array{Num})
end```
````

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 18, 2023, 12:31pm UTC](https://discourse.julialang.org/t/trying-to-understand-parametric-args-in-functions/99052/4 "2023-05-18T12:31:14Z")

</div>

You could also declare a `Union`.

```julia
const G = Union{String, Array{Num}}

function ga(s::String,g::G)
...
end

# alternatively, to specialize on a type

function ga(s::String, g::T) where T <: G
...
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [May 18, 2023, 12:32pm UTC](https://discourse.julialang.org/t/trying-to-understand-parametric-args-in-functions/99052/5 "2023-05-18T12:32:54Z")

</div>

> [@mkitti](#):
>
> `# alternatively, to specialize on a type`

If I’m not mistaken there is no difference in the specialization of those two alternatives. Both will specialize to the given concrete type.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 18, 2023, 12:43pm UTC](https://discourse.julialang.org/t/trying-to-understand-parametric-args-in-functions/99052/6 "2023-05-18T12:43:08Z")

</div>

What I meant is if you wanted to do something with the type parameter, `T`.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [May 18, 2023, 2:23pm UTC](https://discourse.julialang.org/t/trying-to-understand-parametric-args-in-functions/99052/7 "2023-05-18T14:23:37Z")

</div>

> [@brombo](#):
>
> ```julia
> function ga(a::String,g::String)
> g = make_array(g)
> return ga(a::String,g::Array{Num})
> end
> 
> ```

I tend to dispatch this way, especially …

```julia
function f(a, b, c)
    # main computation
end
f(inputs::NamedTuple) = f(inputs.a, inputs.b, inputs.c)

```

Is there a performance reason to dispatch on a smaller internal function instead?

> [@jules](#):
>
> You don’t have to, you can dispatch somewhere in the function, too.
> 
> ```julia
> function ga(s::String, g)
> g = make_array(g)
> # other code
> end
> 
> make_array(g::Array{Num}) = g
> make_array(g::String) = [1, 2, 3]
> 
> ```
> 
> In terms of compilation latency, it’s best to do all conversions towards the unified format first, then dispatch to the “real” function doing all the work. Then that one will not be compiled again and again.

The last paragraph here seems to state the opposite of what you are showing in the code?

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [May 18, 2023, 2:30pm UTC](https://discourse.julialang.org/t/trying-to-understand-parametric-args-in-functions/99052/8 "2023-05-18T14:30:27Z")

</div>

I guess my question is more succinctly which method is better to define?

```julia
make_array(g::Array{Num}) = g
# or 
ga(a::String, g::String) = ga(a, make_array(g))

```

The latter seems more intuitive to me.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [May 18, 2023, 2:33pm UTC](https://discourse.julialang.org/t/trying-to-understand-parametric-args-in-functions/99052/9 "2023-05-18T14:33:32Z")

</div>

> [@Nathan\_Boyer](#):
>
> The last paragraph here seems to state the opposite of what you are showing in the code?

Yes I was trying to say don’t necessarily do it like this 🙂

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [May 18, 2023, 2:34pm UTC](https://discourse.julialang.org/t/trying-to-understand-parametric-args-in-functions/99052/10 "2023-05-18T14:34:11Z")

</div>

Yeah get conversion out of the way as early as possible
