# Functions with 2 generic types

**URL:** <https://discourse.julialang.org/t/functions-with-2-generic-types/15438>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 24, 2018, 8:56pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438 "2018-09-24T20:56:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [September 24, 2018, 8:56pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438/1 "2018-09-24T20:56:30Z")

</div>

I’m trying to have a function with 2 inputs with different generic types, so I wrote the below:

```julia
function mysum(x::T, y::V) where {T, V}
       x+y
end

```

It worked fine with number, but it accept also `Bool` inputs, how can I avoid this, and tell ensure the function accept numbers only!

 ![Capture](https://global.discourse-cdn.com/julialang/original/3X/2/a/2a2e8402d3db7c750ad55284cdf922bb8f38d54c.png)

---

<div class="post-metadata">

**Author:** ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)\
**Post date:** [September 24, 2018, 9:00pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438/2 "2018-09-24T21:00:42Z")

</div>

You can add type constraints to your generic types.

```julia
function mysum(x::T, y::V) where {T<:Number, V<:Number}
       x+y
end

```

But in this case the easiest way to write this would simply be

```julia
function mysum(x::Number, y::Number)
       x+y
end

```

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [September 24, 2018, 9:06pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438/3 "2018-09-24T21:06:28Z")

</div>

both solution still accept boolian s input ☹

---

<div class="post-metadata">

**Author:** ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)\
**Post date:** [September 24, 2018, 9:13pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438/4 "2018-09-24T21:13:48Z")

</div>

Oh that’s true, since `Boolean<:Number` in Julia

```julia
julia> 1 + true
2

julia> 1 + false
1

julia> 2.0 * true
2.0

julia> 2.0 * false
0.0

```

So this is in some sense what you want.

---

<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:** [September 24, 2018, 9:19pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438/5 "2018-09-24T21:19:26Z")

</div>

If you really don’t want booleans in your function, you could define a method

```julia
mySum(x::Bool, y:: Number) = error("Needs non-booleans!") 
mySum(x::Number, y::Bool) = error("Needs non-booleans!") 

```

All other numbers will work then.

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [September 24, 2018, 9:20pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438/6 "2018-09-24T21:20:07Z")

</div>

Actually what I need I is to get an error, not Zero, if any of the 2 inputs is not a number (I mean real number that can be manipulated)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 24, 2018, 9:23pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438/7 "2018-09-24T21:23:11Z")

</div>

Can you say exactly what sort of numbers you want, then? Should complex numbers be accepted? Booleans are numbers, so why not them? Can you specify?

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [September 24, 2018, 9:27pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438/8 "2018-09-24T21:27:11Z")

</div>

I mean by numbers any thing from 0123456789 regardless Int, Float,UInt,…

---

<div class="post-metadata">

**Author:** ![hasanOryx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hasanoryx/32/5373_2.png) [@hasanOryx](https://discourse.julialang.org/u/hasanOryx)\
**Post date:** [September 24, 2018, 9:27pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438/9 "2018-09-24T21:27:59Z")

</div>

If I want to do this for more than a function it will be a headache; thanks for the try.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 24, 2018, 9:30pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438/10 "2018-09-24T21:30:00Z")

</div>

> [@hasanOryx](#):
>
> I mean by numbers any thing from 0123456789 regardless Int, Float,UInt,…

But `Bool` belongs to that set. Could you give a definition of the numbers you want, or is it “anything except `Bool`”?

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [September 24, 2018, 10:32pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438/11 "2018-09-24T22:32:55Z")

</div>

A quick (not so elegant) fix, but it works:

```julia
julia> function mysum(x::T, y::V) where {T<:Union{Signed, Unsigned, AbstractFloat, AbstractIrrational, Rational}} where {V<:Union{Signed, Unsigned, AbstractFloat, AbstractIrrational, Rational}}
         x + y
       end
mysum (generic function with 1 method)

julia> mysum(2,3)
5

julia> mysum(2,3.2)
5.2

julia> mysum(2,true)
ERROR: MethodError: no method matching mysum(::Int64, ::Bool)
Closest candidates are:
  mysum(::T<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}, ::V<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}) where {V<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}, T<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}} at REPL[13]:2
Stacktrace:
 [1] top-level scope at none:0

```

Edit: It doesn’t cover all cases though!

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [September 24, 2018, 11:41pm UTC](https://discourse.julialang.org/t/functions-with-2-generic-types/15438/12 "2018-09-24T23:41:50Z")

</div>

I think this one works:

```julia
julia> const U = Union{Signed, Unsigned, AbstractFloat, AbstractIrrational, Rational}
Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}

julia> function mysum(x::T, y::V) where {T<:U,V<:U}
         x + y
       end
mysum (generic function with 1 method)

julia> mysum(2,3)
5

julia> mysum(2,3.2)
5.2

julia> mysum(2.2,3.2)
5.4

julia> mysum(2.2,3)
5.2

julia> mysum(2.2,true)
ERROR: MethodError: no method matching mysum(::Float64, ::Bool)
Closest candidates are:
  mysum(::T<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}, ::V<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}) where {T<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}, V<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}} at REPL[2]:2
Stacktrace:
 [1] top-level scope at none:0

julia> mysum(true,2.2)
ERROR: MethodError: no method matching mysum(::Bool, ::Float64)
Closest candidates are:
  mysum(::T<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}, ::V<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}) where {T<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}, V<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}} at REPL[2]:2
Stacktrace:
 [1] top-level scope at none:0

```

You can check methods:

```julia
julia> methods(mysum)
# 1 method for generic function "mysum":
[1] mysum(x::T, y::V) where {T<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}, V<:Union{AbstractIrrational, AbstractFloat, Signed, Unsigned, Rational}} in Main at REPL[2]:2

```
