# Adding constrain on the range of \`N\` in parametric types for function arguments

**URL:** <https://discourse.julialang.org/t/adding-constrain-on-the-range-of-n-in-parametric-types-for-function-arguments/65994>\
**Category:** General Usage\
**Tags:** parametric-types, function\
**Created:** [August 7, 2021, 7:02pm UTC](https://discourse.julialang.org/t/adding-constrain-on-the-range-of-n-in-parametric-types-for-function-arguments/65994 "2021-08-07T19:02:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)\
**Post date:** [August 7, 2021, 7:02pm UTC](https://discourse.julialang.org/t/adding-constrain-on-the-range-of-n-in-parametric-types-for-function-arguments/65994/1 "2021-08-07T19:02:47Z")

</div>

I’d like to know if it’s possible to set a range of `N` in a parametric type PT{T, N} when it’s the type of an argument for a defined function.

E.g.

```julia
f(a::Array{<:Number, N}) where {N} = a

```

and I want to set some constrain on `N` such as `N < 4`.

I know I can explicitly write down something like

```julia
f(a::Union{Array{<:Number, 1}, Array{<:Number, 2}, Array{<:Number, 3}}) where {N} = a

```

but I’d like to know if there’s some more efficient way. Thank you!

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [August 7, 2021, 7:08pm UTC](https://discourse.julialang.org/t/adding-constrain-on-the-range-of-n-in-parametric-types-for-function-arguments/65994/2 "2021-08-07T19:08:40Z")

</div>

How about

```julia
Union{(Array{<:Number, i} for i in 1:4)...}

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 7, 2021, 7:10pm UTC](https://discourse.julialang.org/t/adding-constrain-on-the-range-of-n-in-parametric-types-for-function-arguments/65994/3 "2021-08-07T19:10:20Z")

</div>

AFAIK the answer is no. Basically, despite `N` looks like a number, it’s really just a single type no different than `Int64` or `Float64`. Think of it as `Val{N}`. Because of this, compiler doesn’t do “math” on types, only `<:` and `:>`, which is why the answer is probably no.

---

<div class="post-metadata">

**Author:** ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)\
**Post date:** [August 7, 2021, 7:17pm UTC](https://discourse.julialang.org/t/adding-constrain-on-the-range-of-n-in-parametric-types-for-function-arguments/65994/4 "2021-08-07T19:17:27Z")

</div>

Yeah, I think this is as close as we can get here. Thank you!

---

<div class="post-metadata">

**Author:** ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)\
**Post date:** [August 7, 2021, 7:27pm UTC](https://discourse.julialang.org/t/adding-constrain-on-the-range-of-n-in-parametric-types-for-function-arguments/65994/5 "2021-08-07T19:27:13Z")

</div>

Yeah. I get that `N` is still treated as another type in the type system so that we can only do type operations such as `<:` and `:>` here. I wish we could treat it as a primitive-type variable but maybe that will break the function initialization during the compile time? I’m not sure if it’s a design choice or some feature that can be added in the future.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 10, 2021, 11:03am UTC](https://discourse.julialang.org/t/adding-constrain-on-the-range-of-n-in-parametric-types-for-function-arguments/65994/6 "2021-08-10T11:03:57Z")

</div>

If this is for _dispatch_, I would go with a trait, eg a variant of

```julia
in_range(::Array{T,N}) where {T<:Number,N} = Val(N < 4)

_f(::Val{true}, a) = "in range"

f(a) = _f(in_range(a), a)

```

(note that it is good style to allow `<:AbstractArray` in most Julia code).

If this is for _validation_, consider a wrapper type that checks `N` in its inner constructor.

---

<div class="post-metadata">

**Author:** ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)\
**Post date:** [August 12, 2021, 9:59pm UTC](https://discourse.julialang.org/t/adding-constrain-on-the-range-of-n-in-parametric-types-for-function-arguments/65994/7 "2021-08-12T21:59:12Z")

</div>

Yeah, I meant to use it for multiple dispatches. This also seems like a good workaround. Thanks!
