# Constrained type parameters

**URL:** https://discourse.julialang.org/t/constrained-type-parameters/50263
**Category:** New to Julia
**Created:** [November 16, 2020, 11:44pm UTC](https://discourse.julialang.org/t/constrained-type-parameters/50263 "2020-11-16T23:44:55Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![eckofoid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eckofoid/32/221638_2.png) [@eckofoid](https://discourse.julialang.org/u/eckofoid)
#### Post date: [November 16, 2020, 11:44pm UTC](https://discourse.julialang.org/t/constrained-type-parameters/50263/1 "2020-11-16T23:44:55Z")

</div>

When defining a type, is it possible to place a constraint on parameter values? For example, consider the following simple type definition:

```julia
struct Orf
   start::Int32
   stop::Int32
   pol::Int8
end

```

Is there any way to add a hint such as `"stop > start"` to this definition? In pseudo-code, it might resemble

```julia
struct Orf
   start::Int32
   stop::Int32, start > start
   pol::Int8 
end

```

In this way, data errors could be caught without explicit tests in code.

---

<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: [November 16, 2020, 11:52pm UTC](https://discourse.julialang.org/t/constrained-type-parameters/50263/2 "2020-11-16T23:52:12Z")

</div>

Check this, the first example is exactly what you need:

[https://docs.julialang.org/en/v1/manual/constructors/#man-inner-constructor-methods](https://docs.julialang.org/en/v1/manual/constructors/#man-inner-constructor-methods)

---

<div class="post-metadata">

### Author: ![eckofoid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eckofoid/32/221638_2.png) [@eckofoid](https://discourse.julialang.org/u/eckofoid)
#### Post date: [November 17, 2020, 8:12pm UTC](https://discourse.julialang.org/t/constrained-type-parameters/50263/3 "2020-11-17T20:12:18Z")

</div>

Perfect! Thanks very much for pointing this out.

---

<div class="post-metadata">

### Author: ![FedericoStra](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@FedericoStra](https://discourse.julialang.org/u/FedericoStra)
#### Post date: [November 23, 2020, 11:30pm UTC](https://discourse.julialang.org/t/constrained-type-parameters/50263/4 "2020-11-23T23:30:10Z")

</div>

A pattern that I like is

```julia
struct OrderedPair
    x::Real
    y::Real
    function OrderedPair(x, y, ::Val{Checks}=Val(true)) where {Checks}
        if Checks
            x > y || error("out of order")
        end
        new(x, y)
    end
end

```

This way you can avoid the checks when you are 100% sure that the values you are feeding to the constructor satisfy all the requirements. If you call `OrderedPair(x, y, Val(false))`, the checks are statically removed.

---

<div class="post-metadata">

### Author: ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)
#### Post date: [December 18, 2020, 8:08pm UTC](https://discourse.julialang.org/t/constrained-type-parameters/50263/5 "2020-12-18T20:08:42Z")

</div>

This seems to be a perfect use case for the macro @assert from the package Parameters.jl, such as:

```julia
using Parameters
struct OrderedPair
    x::Real
    y::Real; @assert x <= y
end

```

---

<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: [December 19, 2020, 7:13am UTC](https://discourse.julialang.org/t/constrained-type-parameters/50263/6 "2020-12-19T07:13:54Z")

</div>

Note that `@assert` is in `Base`. Perhaps you meant `Parameters.@with_kw`.

---

<div class="post-metadata">

### Author: ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)
#### Post date: [December 19, 2020, 6:36pm UTC](https://discourse.julialang.org/t/constrained-type-parameters/50263/7 "2020-12-19T18:36:21Z")

</div>

Thanks for the correction, Tamas\_Papp: I really meant what you said and wanted to call attention to the very useful (at least to me, as a newbie) Parameters.jl package.
