# How to add constraints to parametric arguments

**URL:** https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231
**Category:** New to Julia
**Created:** [February 13, 2021, 10:23pm UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231 "2021-02-13T22:23:07Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Guillaume\_Leclerc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillaume_leclerc/32/21940_2.png) [@Guillaume\_Leclerc](https://discourse.julialang.org/u/Guillaume_Leclerc)
#### Post date: [February 13, 2021, 10:23pm UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231/1 "2021-02-13T22:23:08Z")

</div>

Hello,

Still trying to wrap my head around the type system of Julia.

Any explanation why this is not allowed, and what would be the best way to achieve something similar?

```julia
struct State{T::Int}
end

```

Is the only way to achieve that through conditions in the inner constructor? If yes, why is that because it would let users “materialize” type that are not constructible. It might be better to raise the error earlier.

Thank you for your help!

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [February 13, 2021, 10:36pm UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231/2 "2021-02-13T22:36:11Z")

</div>

I am not sure if you mean:

```julia
struct State{T<:Int}
    x::T       
end

```

because

> [@Guillaume\_Leclerc](#):
>
> Is the only way to achieve that

achieve what?

---

<div class="post-metadata">

### Author: ![Guillaume\_Leclerc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillaume_leclerc/32/21940_2.png) [@Guillaume\_Leclerc](https://discourse.julialang.org/u/Guillaume_Leclerc)
#### Post date: [February 13, 2021, 10:49pm UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231/3 "2021-02-13T22:49:22Z")

</div>

No I don’t mean that. I want the parametric parameter T to be an Int not a DataType that is a subset of Int.

---

<div class="post-metadata">

### Author: ![EP-Guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ep-guy/32/3442_2.png) [@EP-Guy](https://discourse.julialang.org/u/EP-Guy)
#### Post date: [February 13, 2021, 10:54pm UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231/4 "2021-02-13T22:54:05Z")

</div>

If you want it to always be an `Int`, just specify that it is an `Int` – no need to parameterize.

```julia
struct State
    x::Int   
end

```

---

<div class="post-metadata">

### Author: ![Guillaume\_Leclerc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillaume_leclerc/32/21940_2.png) [@Guillaume\_Leclerc](https://discourse.julialang.org/u/Guillaume_Leclerc)
#### Post date: [February 13, 2021, 10:56pm UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231/5 "2021-02-13T22:56:31Z")

</div>

I know that. But what if my struct needs a field that is a SVector of size T?

I was trying to make my question as simple as possible. I just want to understand better the type system.

---

<div class="post-metadata">

### Author: ![EP-Guy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ep-guy/32/3442_2.png) [@EP-Guy](https://discourse.julialang.org/u/EP-Guy)
#### Post date: [February 13, 2021, 11:08pm UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231/6 "2021-02-13T23:08:10Z")

</div>

You can look at how they do it for `SArray` in their [source code](https://github.com/JuliaArrays/StaticArrays.jl/blob/59f92e0ca7ac391a850a6e7a2ce1eb53aa237fc4/src/SArray.jl#L22). Like you suggested they just use a check in the inner constructor (`check_array_parameters`), which is defined [here](https://github.com/JuliaArrays/StaticArrays.jl/blob/59f92e0ca7ac391a850a6e7a2ce1eb53aa237fc4/src/util.jl#L48).

I’m not sure of an alternative way of doing this.

---

<div class="post-metadata">

### Author: ![Guillaume\_Leclerc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillaume_leclerc/32/21940_2.png) [@Guillaume\_Leclerc](https://discourse.julialang.org/u/Guillaume_Leclerc)
#### Post date: [February 13, 2021, 11:11pm UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231/7 "2021-02-13T23:11:00Z")

</div>

Thanks! Yeah I realized when I tried to do:

```julia
T = SVector{pi}

```

And it worked.

Strangely though

```julia
T2 = SVector{"Test"}

```

Fails

---

<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: [February 13, 2021, 11:33pm UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231/8 "2021-02-13T23:33:56Z")

</div>

> [@Guillaume\_Leclerc](#):
>
> Strangely though
> 
> ```julia
> T2 = SVector{"Test"}
> 
> ```
> 
> Fails

The reason is for this behavior is described in [Manual \> Types \> “Value Types”](https://docs.julialang.org/en/v1.5.3/manual/types/#%22Value-types%22):

> In Julia, you can’t dispatch on a _value_ such as `true` or `false` . However, you can dispatch on parametric types, and Julia allows you to include “plain bits” values (Types, Symbols, Integers, floating-point numbers, tuples, etc.) as type parameters. A common example is the dimensionality parameter in `Array{T,N}` , where `T` is a type (e.g., [`Float64`](https://docs.julialang.org/en/v1.5.3/base/numbers/#Core.Float64)) but `N` is just an `Int` .

You can use `Symbol`s but not `String`s. None of the two are a “plain bits” type, `Symbol` is kinda of the only exception to this rule, but `String` follows the rule: it is not a “plain bits” type and therefore its _values_ **cannot** be used as type parameters.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [February 13, 2021, 11:43pm UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231/9 "2021-02-13T23:43:45Z")

</div>

The constraint you want must be expressed with an inner constructor, it can’t be imposed on a type level.

```julia
julia> struct State{T}
           State{T}() where {T} = T isa Int ? new{T}() : error("Only ints allowed")
       end

julia> State{1}()
State{1}()

```

This has the consequence that users can always refer to the hypothetical type `State{1.0}`, but not to it’s instances:

```julia
julia> State{1.0}
State{1.0}

julia> State{1.0}()
ERROR: Only ints allowed
Stacktrace:
...

```

I do sometimes wish we could do the constraints you’re asking for. I remember @jameson explained to me once that it’s a good thing you can create these hypothetical types. I’m not sure I found the explanation very satisfying, but I do place a lot of trust in his insight.

---

<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: [February 13, 2021, 11:52pm UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231/10 "2021-02-13T23:52:32Z")

</div>

From your other thread, isn’t this what you want?

```julia
julia> using StaticArrays

julia> struct Test{N,T} 
         x :: SVector{N,T}
       end

julia> Test(SVector{3,Int}(0,0,0))
Test{3,Int64}([0, 0, 0])

```

That will fail if `N` is not an integer, because of the constructor for the `SVector`. (But as pointed above the constraint, if you want to anticipate it, has to be written in the inner constructor).

---

<div class="post-metadata">

### Author: ![Guillaume\_Leclerc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillaume_leclerc/32/21940_2.png) [@Guillaume\_Leclerc](https://discourse.julialang.org/u/Guillaume_Leclerc)
#### Post date: [February 14, 2021, 12:37am UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231/11 "2021-02-14T00:37:26Z")

</div>

It felt natural to me that we could add these constraints too.

Having types there would allow unpacking too maybe: Something like this:

```julia
struct{A::Tuple{U, U}} where {U}
....
end

```

Or something along those lines

---

<div class="post-metadata">

### Author: ![Guillaume\_Leclerc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guillaume_leclerc/32/21940_2.png) [@Guillaume\_Leclerc](https://discourse.julialang.org/u/Guillaume_Leclerc)
#### Post date: [February 14, 2021, 12:38am UTC](https://discourse.julialang.org/t/how-to-add-constraints-to-parametric-arguments/55231/12 "2021-02-14T00:38:12Z")

</div>

Yes this is exactly what I wanted. I was just wondering if there was a way to write it in a more “Strongly typed” manner.
