# Need a little help with parametric types

**URL:** <https://discourse.julialang.org/t/need-a-little-help-with-parametric-types/50481>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 20, 2020, 9:50am UTC](https://discourse.julialang.org/t/need-a-little-help-with-parametric-types/50481 "2020-11-20T09:50:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [November 20, 2020, 9:50am UTC](https://discourse.julialang.org/t/need-a-little-help-with-parametric-types/50481/1 "2020-11-20T09:50:42Z")

</div>

I want to prepare a example but aren’t able to create it. Can someone help me 🙂

```julia
julia> struct ModInt{N <: Integer} <: Integer
           k::Integer
           ModInt{N}(k::Integer) where {N <: Integer} = new(mod(k,N))
       end

julia> ModInt{2}(1)
ERROR: TypeError: in ModInt, in N, expected N<:Integer, got a value of type Int64
Stacktrace:
 [1] top-level scope at REPL[2]:1

```

Thank you!

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 20, 2020, 10:46am UTC](https://discourse.julialang.org/t/need-a-little-help-with-parametric-types/50481/2 "2020-11-20T10:46:17Z")

</div>

You cannot place a type annotation on a field that will be a value/object (2 is a value, not a type).

First consider whether it makes sense to have `ModInt` have a unique type for each mod that it’s doing (I assume that’s what it’s for). The answer of course is that it makes no sense at all 😃 and you should just have a second field with the `N` parameter.

But if this is just an exercise in values-as-parameters, the way to handle this is to remove the type annotation, and use an inner constructor that checks if `N` is an integer.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [November 20, 2020, 11:01am UTC](https://discourse.julialang.org/t/need-a-little-help-with-parametric-types/50481/3 "2020-11-20T11:01:02Z")

</div>

I want to replicate this example:

[![](https://global.discourse-cdn.com/julialang/original/3X/9/a/9a5a927dd06d4f8c44dd82dd64ad037ae0c5170e.jpeg "Rationale Behind Julia and the Vision (Jeff Bezanson, Stefan Karpinski)") ](https://www.youtube.com/watch?v=02U9AJMEWx0&t=439)

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 20, 2020, 11:08am UTC](https://discourse.julialang.org/t/need-a-little-help-with-parametric-types/50481/4 "2020-11-20T11:08:35Z")

</div>

That’s a VERY old video. Keep in mind julia 0.3 was out in 2014, and this is year older than even _that_! I suggest referring to the julia manual as an up to date resource (it features a section on parametric types, of course).

Also note that in the video, there was no type annotation on `n`. That is, in the definition line, simply: `struct ModInt{N} <: Integer` was used.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [November 20, 2020, 11:19am UTC](https://discourse.julialang.org/t/need-a-little-help-with-parametric-types/50481/5 "2020-11-20T11:19:18Z")

</div>

```julia
julia> struct ModInt{N} <: Integer
           k::Integer
           ModInt{N}(k::Integer) where {N} = new(mod(k,N))
       end

julia> ModInt{2}(1)
ModInt{2}(1)

julia> ModInt{2.1}(1)
ModInt{2.1}(1)

```

This works but I want to restrict the `N` to only be `Int`.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 20, 2020, 12:10pm UTC](https://discourse.julialang.org/t/need-a-little-help-with-parametric-types/50481/6 "2020-11-20T12:10:14Z")

</div>

You can do that in the inner constructor:

```julia
julia> struct ModInt{N} <: Integer
           k::Integer
           ModInt{N}(k::Integer) where N = N isa Integer ? new(mod(k,N)) : error("helpful error")
       end

```

By the way, `Integer` is an abstract type, so code for `ModInt` will be poorly optimized. Use

```julia
k::Int

```

instead
