Reputation: 9572
It is a cool feature of Julia that values can be used as types, at least as type parameters. For example, one can assert that arrays are of a particular dimensionality, such as x :: Array{Int,2}
. My question is: how does Julia do that and how do users of Julia get access to that power? I assume that 2
is being converted to or interpreted as some sort of singleton type of 2
. I am curious to know what function does that conversion. I tried to assert 2 :: Type{2}
and isa(2, Type{2})
, but that only asserts a singleton if 2
is replaced by an actual type.
Upvotes: 3
Views: 553
Reputation: 3783
You can not define your own imutables and use them as singleton types (yet).
Currently anything that makes static int valid_type_param(jl_value_t *v)
defined in jltypes.c return true, can be used as a type parameter. There is a TODO to add more types, and you'll probably just need a compelling usecase to get help to change the behaviour.
Update: See also the manual documentation on types: Both abstract and concrete types can be paramaterized by other types and by certain other values (currently integers, symbols, bools, and tuples thereof). Type parameters may be completely omitted when they do not need to be referenced or restricted.
Upvotes: 3