# How do I attach size to a type?

**URL:** https://discourse.julialang.org/t/how-do-i-attach-size-to-a-type/55614
**Category:** General Usage
**Created:** [February 19, 2021, 12:57pm UTC](https://discourse.julialang.org/t/how-do-i-attach-size-to-a-type/55614 "2021-02-19T12:57:56Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![minetest2048](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/minetest2048/32/45961_2.png) [@minetest2048](https://discourse.julialang.org/u/minetest2048)
#### Post date: [February 19, 2021, 12:57pm UTC](https://discourse.julialang.org/t/how-do-i-attach-size-to-a-type/55614/1 "2021-02-19T12:57:57Z")

</div>

I want to make a statically sized bit array, something like

```julia
Bits{5} = [0,0,0,0,0]
Bits{N} = [0,...] with length of N

```

I want to restrict N to an integer. What do I put in the struct type declaration?

```julia
struct Bits{???}
    storage
end

```

I’ve tried:

1. `Bits{N<:Integer}`
2. `Bits{N<:Tuple}`
3. `Bits{N<:Tuple{<:Integer}}`
4. `abstract type Bits{N} end`
5. `abstract type Bits5{N<:Integer} end`
6. `Bits{N<:BaseType}`

1 can be declared, but when I try to use the struct it errors:

```julia
julia> Bits{4}
ERROR: TypeError: in Bits, in N, expected N<:Integer, got Int64

```

2 kind of works, but then Tuple type can accept everything

```julia
julia> Bits2{Tuple{2,pi}}
Bits2{Tuple{2,π}}

```

3 fails like 1

```julia
julia> Bits3{Tuple{4}}
ERROR: TypeError: in Bits3, in N, expected N<:(Tuple{#s1} where #s1<:Integer), got Type{Tuple{4}}

```

4 kinda works but it can accept everything

```julia
julia> Bits4{pi}
Bits4{π}

```

5 fails

```julia
julia> Bits5{10}
ERROR: TypeError: in Bits5, in N, expected N<:Integer, got Int64

```

6 can’t be declared

```julia
julia> struct Bits6{N<:BaseType}
           asdf
       end
ERROR: UndefVarError: BaseType not defined

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 19, 2021, 1:02pm UTC](https://discourse.julialang.org/t/how-do-i-attach-size-to-a-type/55614/2 "2021-02-19T13:02:36Z")

</div>

> [@minetest2048](#):
>
> `Bits{N<:Integer}`

This is saying that `N` is a _type_ that is a _subtype_ of `Integer`. You want `N::Integer` (`N` is an _instance_ of `Integer`), but AFAIK there is no way to declare this for type parameters.

Just declare `Bits{N}` and then enforce `N` being an integer in the constructor. See also how [StaticArrays.jl does it](https://github.com/JuliaArrays/StaticArrays.jl/blob/59f92e0ca7ac391a850a6e7a2ce1eb53aa237fc4/src/SArray.jl#L18-L30).
