# Parametrize a struct with numbers not types

**URL:** <https://discourse.julialang.org/t/parametrize-a-struct-with-numbers-not-types/108725>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 12, 2024, 1:31pm UTC](https://discourse.julialang.org/t/parametrize-a-struct-with-numbers-not-types/108725 "2024-01-12T13:31:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 12, 2024, 1:31pm UTC](https://discourse.julialang.org/t/parametrize-a-struct-with-numbers-not-types/108725/1 "2024-01-12T13:31:14Z")

</div>

Structs parametrized with types are common,

```julia
struct coo{T} where {T}
   x::T
   y::T
end

```

Is it possible to parametrize a struct with numbers, like integers? For example, suppose I want to classify all n-component vectors as a struct named ‘coordinates’, with a parameter ‘n’ indicating the number of components, how can I do?

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [January 12, 2024, 1:39pm UTC](https://discourse.julialang.org/t/parametrize-a-struct-with-numbers-not-types/108725/2 "2024-01-12T13:39:22Z")

</div>

I’m not the best person to answer your question, though I brought some inspiration from Meshes.jl’s [primitives](https://github.com/JuliaGeometry/Meshes.jl/blob/master/src/primitives.jl) and [points](https://github.com/JuliaGeometry/Meshes.jl/blob/master/src/primitives/point.jl).

They have roughly the following:

```julia
abstract type Primitive{Dim,T} <: Geometry{Dim,T} end

struct Point{Dim,T} <: Primitive{Dim,T}
  coords::Vec{Dim,T}
  Point(coords::Vec{Dim,T}) where {Dim,T} = new{Dim,T}(coords)
end

const Point1 = Point{1,Float64}

```

You can find more details in the linked files (and I learned a lot from this package as a whole as well). Hope this helps.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [January 12, 2024, 1:41pm UTC](https://discourse.julialang.org/t/parametrize-a-struct-with-numbers-not-types/108725/3 "2024-01-12T13:41:54Z")

</div>

Yes, you can have data as parameters, provided the data is “bits” - i.e. contain no pointers, and is not mutable.  
The built-in type `BitArray`, for example, have a number as a type parameter:

```julia
julia> BitArray{2}(undef, (3, 5))
3×5 BitMatrix:
 0 0 0 0 0
 0 0 0 0 0
 0 0 0 0 0

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 12, 2024, 4:13pm UTC](https://discourse.julialang.org/t/parametrize-a-struct-with-numbers-not-types/108725/4 "2024-01-12T16:13:36Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> Is it possible to parametrize a struct with numbers, like integers?

Any type, too. The most immediately encountered example is `Array{T, N}`; `Vector{T}` is an alias for `Array{T, 1}`.

> [@jiang\_ming\_zhang](#):
>
> I want to classify all n-component vectors as a struct named ‘coordinates’, with a parameter ‘n’ indicating the number of components, how can I do?

There is a difficulty there because a `struct` definition can’t vary the number of fields to match a parameter value (after all, how could you _name_ them at definition if they vary). However, you could have 1 field of the struct hold a `NTuple{n, T}`, which is a special alias for `Tuple` that allows you to specify number of elements instead of each type, precisely for cases like this. However, for `Vector`-like functionality, you’ll want to use `SVector` or some other type from StaticArrays.jl, which are internally based on tuples. Bear in mind that this approach is best when you have few values for `n` and they are small; if `n` varies a lot or gets large, it hurts rather than helps performance. StaticArrays.jl suggests 100 elements at most, which far more than enough for most use cases involving coordinates.

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [January 12, 2024, 6:38pm UTC](https://discourse.julialang.org/t/parametrize-a-struct-with-numbers-not-types/108725/5 "2024-01-12T18:38:56Z")

</div>

I implemented a similar idea [here](https://github.com/mikeingold/UnitfulCoordinateSystems.jl/blob/master/src/structs.jl) where I have an abstract type for all coordinates with a parameter N to indicate the number of dimensions that each coordinate type occupies.
