# Restrict type of value type parameter

**URL:** <https://discourse.julialang.org/t/restrict-type-of-value-type-parameter/12127>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 3, 2018, 1:44pm UTC](https://discourse.julialang.org/t/restrict-type-of-value-type-parameter/12127 "2018-07-03T13:44:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 3, 2018, 1:44pm UTC](https://discourse.julialang.org/t/restrict-type-of-value-type-parameter/12127/1 "2018-07-03T13:44:47Z")

</div>

I am declaring a parametric type `struct A{L} end`, where `L` should be an integer. For example, in `NTuple{N,T}`, the `N` parameter is restricted to be an integer. What is the recommended way to achieve this behavior on my custom time? I could not find the source code of `NTuple`.

---

<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:** [July 3, 2018, 2:27pm UTC](https://discourse.julialang.org/t/restrict-type-of-value-type-parameter/12127/2 "2018-07-03T14:27:44Z")

</div>

I think that `NTuple` _per se_ does not check that `N` is an integer, `Vararg` (which it is aliased to) does.

For `struct`s, the recommended solution is to check in the inner constructor, eg

```julia
struct A{L}
    function A{L}() where L
        @assert L isa Integer
        new{L}()
    end
end

A(L::Integer) = A{L}() # convenience constructor

A(1) # OK
A("a fish") # error

```

---

<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:** [July 3, 2018, 2:30pm UTC](https://discourse.julialang.org/t/restrict-type-of-value-type-parameter/12127/3 "2018-07-03T14:30:36Z")

</div>

PS: the source of `NTuple` is [in boot.jl](https://github.com/JuliaLang/julia/blob/175e6744b4bc9572a3575ff7946ec1e1d83ae553/base/boot.jl#L387). It is nothing more than

```julia
const NTuple{N,T} = Tuple{Vararg{T,N}}

```

There is no magic. Being able to write such core constructs in Julia lends a certain elegance to the language.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 3, 2018, 2:52pm UTC](https://discourse.julialang.org/t/restrict-type-of-value-type-parameter/12127/5 "2018-07-03T14:52:35Z")

</div>

Thanks. If `Vararg` is also in Julia, do you know where is the source?

---

<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:** [July 3, 2018, 2:54pm UTC](https://discourse.julialang.org/t/restrict-type-of-value-type-parameter/12127/6 "2018-07-03T14:54:35Z")

</div>

I think that `Vararg` is implemented in C.

---

<div class="post-metadata">

**Author:** ![aitzkora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aitzkora/32/14610_2.png) [@aitzkora](https://discourse.julialang.org/u/aitzkora)\
**Post date:** [March 2, 2021, 2:44pm UTC](https://discourse.julialang.org/t/restrict-type-of-value-type-parameter/12127/7 "2021-03-02T14:44:31Z")

</div>

Hi, I want to achieve the same goal except that L must limited to three integer values 1,2, 3. Is there a way to that using `Union{Val{1},Val{2},Val{3}}`. In fact, after create the type I want implement dispatch  
on these values like in the constructor of Array{T,N}

Array{T,1}(::UndefInitializer, m::Int) where {T} =  
ccall(:jl\_alloc\_array\_1d, Array{T,1}, (Any, Int), Array{T,1}, m)  
Array{T,2}(::UndefInitializer, m::Int, n::Int) where {T} =  
ccall(:jl\_alloc\_array\_2d, Array{T,2}, (Any, Int, Int), Array{T,2}, m, n)

But Sadly I do not find the definition of Array{T,N} using @edit

Bests

---

<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:** [March 10, 2021, 10:21am UTC](https://discourse.julialang.org/t/restrict-type-of-value-type-parameter/12127/8 "2021-03-10T10:21:23Z")

</div>

It is not clear to me what you are doing (please open a separate topic with an MWE if you need further help). In any case, validating in the constructor is still the recommended method.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![aitzkora](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aitzkora/32/14610_2.png) [@aitzkora](https://discourse.julialang.org/u/aitzkora)\
**Post date:** [March 11, 2021, 10:06pm UTC](https://discourse.julialang.org/t/restrict-type-of-value-type-parameter/12127/9 "2021-03-11T22:06:04Z")

</div>

Hi,  
Effectively, I enter too abruptly into the discussion. Next time, I will follow the rules for discourse 😄 . In addition, my question was not clear. I mixed up two points : restrict the parameter values and enable dispatch respective to the different values of the parameter. Anyway, I found a convienent solution for me and I use your trick for restricting values. Thanks
