# How to constrain the type parameters for this function

**URL:** <https://discourse.julialang.org/t/how-to-constrain-the-type-parameters-for-this-function/91801>\
**Category:** General Usage\
**Created:** [December 18, 2022, 12:29am UTC](https://discourse.julialang.org/t/how-to-constrain-the-type-parameters-for-this-function/91801 "2022-12-18T00:29:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![babaq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/babaq/32/891_2.png) [@babaq](https://discourse.julialang.org/u/babaq)\
**Post date:** [December 18, 2022, 12:29am UTC](https://discourse.julialang.org/t/how-to-constrain-the-type-parameters-for-this-function/91801/1 "2022-12-18T00:29:50Z")

</div>

given an example function

```julia
function foo(a,b)
  ...
end

```

`a` would accept vector of real numbers, such as `Vector{Int64}, Vector{Float64}, ...`  
`b` should be Integer while elements of `a` are Integers, and Integer or Float while elements of `a` are Floats.

Here is a type constrained version

```julia
function foo(a::Vector{T},b::T) where {T<:Real}
  ...
end

```

The problem of this version is that the Integer is not a subtype of AbstractFloat, so that

```julia
foo([1,2,3], 5) is allowed, and foo([1.0,2.0,3.0], 5) is not allowed, but foo([1.0,2.0,3.0], 5.0) is ok.

```

How can i constrain the type parameters so that the desired behavior (last two invoke) be achieved in one method definition, or is it possible to do at all?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 18, 2022, 12:34am UTC](https://discourse.julialang.org/t/how-to-constrain-the-type-parameters-for-this-function/91801/2 "2022-12-18T00:34:25Z")

</div>

I think you want `foo(a::Vector{T1},b::T2) where {T1 <: Real, T2 <: Real}` (or `foo(a::Vector{<:Real},b::Real)` if you don’t need the type parameters).

---

<div class="post-metadata">

**Author:** ![babaq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/babaq/32/891_2.png) [@babaq](https://discourse.julialang.org/u/babaq)\
**Post date:** [December 18, 2022, 12:40am UTC](https://discourse.julialang.org/t/how-to-constrain-the-type-parameters-for-this-function/91801/3 "2022-12-18T00:40:50Z")

</div>

The T2 should be Integer when T1 is Integer.

```julia
foo(a::Vector{<:Real},b::Real) will make
foo([1,2,3], 5.0) allowed, but is not the desired behavior

```

The function would be an algorithm for discrete and continues domains, so when `a` is represented in discrete integers, `b` should also be integer, on the other hand, if `a` is floating points, it doesn’t matter `b` in float or integer. The original type constrained version force `b` be the same floating point type.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [December 18, 2022, 1:08am UTC](https://discourse.julialang.org/t/how-to-constrain-the-type-parameters-for-this-function/91801/4 "2022-12-18T01:08:58Z")

</div>

For the cases you want to throw (e.g. vector of integers with floating point `b`), I would add a check that throws with a helpful error message, or define a method that this case hits to throw a helpful error message.

If the types are known at compile time (i.e. the call to `foo` is typestable), this would have 0 runtime overhead in the cases you support.

If you’d prefer a `MethodError`, and you’re also handling all the cases you do support with the same implementation, then you can make that implementation an unconstrained `_foo`, and have a `foo` for each case that you do support forward there.  
I’d do what is simplest (so long as they have 0 runtime overhead, which is the case for all proposals here), which depends on what `foo`’s implementation looks like.

Like if you need different implementations on input types, it’d make sense to just define those directly, and not define the type combinations you don’t support.

---

<div class="post-metadata">

**Author:** ![babaq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/babaq/32/891_2.png) [@babaq](https://discourse.julialang.org/u/babaq)\
**Post date:** [December 18, 2022, 1:46am UTC](https://discourse.julialang.org/t/how-to-constrain-the-type-parameters-for-this-function/91801/5 "2022-12-18T01:46:08Z")

</div>

so far the implementation is the same, so i wandered if i can do this in one method definition. I’ll use runtime check for now.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [December 18, 2022, 1:54am UTC](https://discourse.julialang.org/t/how-to-constrain-the-type-parameters-for-this-function/91801/6 "2022-12-18T01:54:34Z")

</div>

> [@babaq](#):
>
> I’ll use runtime check for now.

Note that this “runtime check” will have 0 runtime cost, because the compiler will delete it when specializing on the input types.

---

<div class="post-metadata">

**Author:** ![babaq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/babaq/32/891_2.png) [@babaq](https://discourse.julialang.org/u/babaq)\
**Post date:** [December 18, 2022, 2:36am UTC](https://discourse.julialang.org/t/how-to-constrain-the-type-parameters-for-this-function/91801/7 "2022-12-18T02:36:19Z")

</div>

The revised version follows:

```julia
function foo(a::Vector{T},b::T1) where {T<:Real,T1<:Real}
  if T <: Integer && !(T1 <: Integer)
    error("b should be Integer")
  end
end

```
