In “AbstractArray{T,N}
”, I understand T
(element type can be anything), but not N
, the dimensionality. Why is N
not Int
?
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…
N
is a value, not a type. A Vector
is Array{T,1}
, a Matrix
is Array{T, 2}
etc.
Ah, so parameters to types needn’t be types or type variables (like where T
), they may be values as well.
…though not any value:
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}
.
You can enforce it in the constructor.
What would the dimensionality of an Array{Float64, Int}
be?
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.
unfortunately, some times a check in the (inner) constructor is the only way to go do it as Julia doesn’t have dependent type.
Whimsically, we can evaluate Array{Int, 'π'}
no problem.
There are no constructors for it alas.