# What's the correct way of defining struct with an Integer Parameter?

**URL:** <https://discourse.julialang.org/t/whats-the-correct-way-of-defining-struct-with-an-integer-parameter/23974>\
**Category:** New to Julia\
**Created:** [May 7, 2019, 5:57pm UTC](https://discourse.julialang.org/t/whats-the-correct-way-of-defining-struct-with-an-integer-parameter/23974 "2019-05-07T17:57:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)\
**Post date:** [May 7, 2019, 5:57pm UTC](https://discourse.julialang.org/t/whats-the-correct-way-of-defining-struct-with-an-integer-parameter/23974/1 "2019-05-07T17:57:19Z")

</div>

From the document, we know that

> 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.

But how can one actually define such a type? I tried the following and it doesn’t seem to work.

```julia
julia> struct tree{D<:Integer}
           size::Int
       end

julia> t = tree{33}(15)
ERROR: TypeError: in tree, in D, expected D<:Integer, got Int64
Stacktrace:
 [1] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 7, 2019, 6:04pm UTC](https://discourse.julialang.org/t/whats-the-correct-way-of-defining-struct-with-an-integer-parameter/23974/2 "2019-05-07T18:04:46Z")

</div>

Your definition isn’t quite doing what you think, because `D <: Integer` is not the right condition. `D <: Integer` says that `D` is a _type_ which is some subtype of `Integer`, not that `D` is a _value_ whose type is `<: Integer`. For example:

```julia
julia> Int <: Integer
true

julia> Int32 <: Integer
true

julia> 33 <: Integer
ERROR: TypeError: in <:, expected Type, got Int64
Stacktrace:
 [1] top-level scope at none:0

```

Instead, you need to check the condition on `D` inside an inner constructor. See [Restrict type of value type parameter - #2 by Tamas\_Papp](https://discourse.julialang.org/t/restrict-type-of-value-type-parameter/12127/2) for an example.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 7, 2019, 6:18pm UTC](https://discourse.julialang.org/t/whats-the-correct-way-of-defining-struct-with-an-integer-parameter/23974/3 "2019-05-07T18:18:43Z")

</div>

Here is an example:

```julia
struct MyIntField{T}
    myfield::T

    function MyIntField(x::Integer)
         return new{typeof(x)}(x)
    end
end

MyIntField(x) = throw(DomainError("noninteger type"))

a = MyIntField(2)
# MyIntField{Int64}(2)

b = MyIntField(2.0)
# ERROR: DomainError with noninteger type

```

or

```julia
struct MyIntField{T}
    myfield::T

    function MyIntField(x::T) where {T<:Integer}
         return new{T}(x)
    end
end

MyIntField(x) = throw(DomainError("noninteger type"))

```

---

<div class="post-metadata">

**Author:** ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)\
**Post date:** [May 7, 2019, 6:29pm UTC](https://discourse.julialang.org/t/whats-the-correct-way-of-defining-struct-with-an-integer-parameter/23974/4 "2019-05-07T18:29:41Z")

</div>

Thanks I think I understand it now.  
Though I was trying to have a type like MyIntField{2} instead of MyInfField{Int64}. So the code should be

```julia
struct MyStruct{D}
     myfield
 
     function MyStruct{D}(x)
          @assert D isa Integer
          return new{D}(x)
     end
end

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 7, 2019, 6:36pm UTC](https://discourse.julialang.org/t/whats-the-correct-way-of-defining-struct-with-an-integer-parameter/23974/5 "2019-05-07T18:36:40Z")

</div>

fyi: That is not a good way to use parameters – it is highly disrecommended for any purpose other than keeping a count as `NTuple` does.

---

<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:** [May 8, 2019, 4:53am UTC](https://discourse.julialang.org/t/whats-the-correct-way-of-defining-struct-with-an-integer-parameter/23974/6 "2019-05-08T04:53:29Z")

</div>

> [@Xing\_Shi\_Cai](#):
>
> So the code should be

Note that this is fairly suboptimal, as `myfield` has an abstract type (`Any`). You should have a type parameter for it.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [May 8, 2019, 5:55am UTC](https://discourse.julialang.org/t/whats-the-correct-way-of-defining-struct-with-an-integer-parameter/23974/7 "2019-05-08T05:55:28Z")

</div>

> [@JeffreySarnoff](#):
>
> fyi: That is not a good way to use parameters – it is highly disrecommended for any purpose other than keeping a count as `NTuple` does.

I think that is far too strong a statement.  
There are many good reasons to store many things in the type parameters.  
You just have to be aware of the trade-offs.  
Which are roughly:  
**Pro** : you can dispatch on it,  
**Con** : a new instance of every method called on it will have to be compiled.

> [@Xing\_Shi\_Cai](#):
>
> ```struct
> myfield 
> function MyStruct{D}(x)
> @assert D isa Integer
> return new{D}(x) 
> end
> end
> 
> ```

Correct

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 8, 2019, 5:56am UTC](https://discourse.julialang.org/t/whats-the-correct-way-of-defining-struct-with-an-integer-parameter/23974/8 "2019-05-08T05:56:12Z")

</div>

> I think that is far too strong a statement.

I think that is far too strong a statement.

(I was responding to the question in the asker’s context, not responding to all possible worlds)

---

<div class="post-metadata">

**Author:** ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)\
**Post date:** [May 8, 2019, 7:32am UTC](https://discourse.julialang.org/t/whats-the-correct-way-of-defining-struct-with-an-integer-parameter/23974/9 "2019-05-08T07:32:24Z")

</div>

If I keep using just one instance of it, this should be fine, right?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 8, 2019, 9:54am UTC](https://discourse.julialang.org/t/whats-the-correct-way-of-defining-struct-with-an-integer-parameter/23974/10 "2019-05-08T09:54:34Z")

</div>

if you mean there is one instance of your struct and so there is only int value that is used as a parameter, yes – still not clear why you want to do it that way
