# Value as a type parameter, \`X{1}\`

**URL:** https://discourse.julialang.org/t/value-as-a-type-parameter-x-1/74215
**Category:** New to Julia
**Tags:** question, parametric-types
**Created:** [January 7, 2022, 8:50pm UTC](https://discourse.julialang.org/t/value-as-a-type-parameter-x-1/74215 "2022-01-07T20:50:20Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)
#### Post date: [January 7, 2022, 8:50pm UTC](https://discourse.julialang.org/t/value-as-a-type-parameter-x-1/74215/1 "2022-01-07T20:50:21Z")

</div>

In “`AbstractArray{T,N}`”, I understand `T` (element type can be anything), but not `N`, the dimensionality. Why is `N` not `Int`?

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [January 7, 2022, 9:00pm UTC](https://discourse.julialang.org/t/value-as-a-type-parameter-x-1/74215/2 "2022-01-07T21:00:40Z")

</div>

Indeed `Array` is a parsmetric type where the second parameter is an integer. `N` is just the name given to the parameter, it could be `Foo` or whatever…

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [January 7, 2022, 9:01pm UTC](https://discourse.julialang.org/t/value-as-a-type-parameter-x-1/74215/3 "2022-01-07T21:01:24Z")

</div>

`N` is a value, not a type. A `Vector` is `Array{T,1}`, a `Matrix` is `Array{T, 2}` etc.

---

<div class="post-metadata">

### Author: ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)
#### Post date: [January 7, 2022, 9:21pm UTC](https://discourse.julialang.org/t/value-as-a-type-parameter-x-1/74215/4 "2022-01-07T21:21:48Z")

</div>

Ah, so parameters to types needn’t be types or type variables (like `where T`), they may be values as well.

---

<div class="post-metadata">

### Author: ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)
#### Post date: [January 7, 2022, 9:26pm UTC](https://discourse.julialang.org/t/value-as-a-type-parameter-x-1/74215/5 "2022-01-07T21:26:38Z")

</div>

…though not any value:

```julia
julia> struct X{T} end

julia> X{8}
X{8}

julia> X{""}
ERROR: TypeError: in Type, in parameter, expected Type, got a value of type String

```

I suppose only `isbits` values can be used as a type parameter.

Then I guess my question is, is there any way to constrain value type parameters (`where {N isa Int}`?) – just like type type parameters can be constrained with `where {T <: Number}`.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 7, 2022, 9:38pm UTC](https://discourse.julialang.org/t/value-as-a-type-parameter-x-1/74215/6 "2022-01-07T21:38:15Z")

</div>

You can enforce it in the constructor.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [January 7, 2022, 9:40pm UTC](https://discourse.julialang.org/t/value-as-a-type-parameter-x-1/74215/7 "2022-01-07T21:40:53Z")

</div>

> [@tfiers](#):
>
> Why is `N` not `Int` ?

What would the dimensionality of an `Array{Float64, Int}` be?

---

<div class="post-metadata">

### Author: ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)
#### Post date: [January 7, 2022, 9:44pm UTC](https://discourse.julialang.org/t/value-as-a-type-parameter-x-1/74215/8 "2022-01-07T21:44:22Z")

</div>

Yeah I was confused about what `N` was (a type _parameter_, not necessarily a type).  
I was thinking of something like `Array{T,N} where {N isa Int}` – or indeed a constructor check like @kristoffer.carlsson says.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [January 7, 2022, 10:00pm UTC](https://discourse.julialang.org/t/value-as-a-type-parameter-x-1/74215/9 "2022-01-07T22:00:04Z")

</div>

> [@tfiers](#):
>
> something like `Array{T,N} where {N isa Int}` – or indeed a constructor check like @kristoffer.carlsson says.

unfortunately, some times a check in the (inner) constructor is the only way to go do it as Julia doesn’t have dependent type.

---

<div class="post-metadata">

### Author: ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)
#### Post date: [January 7, 2022, 10:59pm UTC](https://discourse.julialang.org/t/value-as-a-type-parameter-x-1/74215/10 "2022-01-07T22:59:17Z")

</div>

Whimsically, we can evaluate `Array{Int, 'π'}` no problem.  
There are no constructors for it alas.
