# Limit argument types of a function

**URL:** <https://discourse.julialang.org/t/limit-argument-types-of-a-function/98415>\
**Category:** New to Julia\
**Tags:** question, parametric-types, functions\
**Created:** [May 6, 2023, 8:34am UTC](https://discourse.julialang.org/t/limit-argument-types-of-a-function/98415 "2023-05-06T08:34:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [May 6, 2023, 8:34am UTC](https://discourse.julialang.org/t/limit-argument-types-of-a-function/98415/1 "2023-05-06T08:34:19Z")

</div>

Hi,  
Is the below example the “best” way to limit the types that can be passed to a function?  
I would like to only allow Int64 and Float64, i.e. I want the function definition to be either vadd(::Float64,::Float64) or vadd(::Float64,::Float64). I don’t want to allow vadd(::Float64,::Int64) for example.

```julia
module TT

const tt = Union{Float64, Int64}

function vadd(a::T, b::T) where T <:tt
  return a+b+oneunit(T)
end

end

```

Thank you

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [May 6, 2023, 8:54am UTC](https://discourse.julialang.org/t/limit-argument-types-of-a-function/98415/2 "2023-05-06T08:54:53Z")

</div>

Looks good to me. You don’t need the alias `tt` but I’m guessing you’ll reuse that union frequently.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [May 6, 2023, 9:33am UTC](https://discourse.julialang.org/t/limit-argument-types-of-a-function/98415/3 "2023-05-06T09:33:32Z")

</div>

You code looks fine. Is there anything in particular you dislike? Depending on your application, it could be that just checking if the types are the same is sufficient, e.g. `vadd(a::T, b::T) where {T}`. That way you code remains a bit more generic. (But like Benny said, nothing wrong with your approach.)

---

<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:** [May 6, 2023, 12:07pm UTC](https://discourse.julialang.org/t/limit-argument-types-of-a-function/98415/4 "2023-05-06T12:07:55Z")

</div>

As a side note, I would capitalize `tt` (so `Tt` or another name starting with uppercase), as it is the identifier of a type, and per style guide they should be capitalized.

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [May 6, 2023, 4:48pm UTC](https://discourse.julialang.org/t/limit-argument-types-of-a-function/98415/5 "2023-05-06T16:48:36Z")

</div>

Thank you guys. Nothing that I dislike. I just wanted to be sure that it is the proper way to do.

---

<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 6, 2023, 7:01pm UTC](https://discourse.julialang.org/t/limit-argument-types-of-a-function/98415/6 "2023-05-06T19:01:53Z")

</div>

I would add that this also solves the problem of the mixed float and int input:

```julia
julia> f(a::T,b::T) where T = 1
f (generic function with 1 method)

julia> f(1, 1.0)
ERROR: MethodError: no method matching f(::Int64, ::Float64)

```

while still remaining general such that `Float32`s, or `Int32` are also accepted, for example (it may be that the definition of the union type is not necessary for what you want).
