I am trying to have custom Integer-types I can use for dispatch on getindex
.
So far I tried the following:
abstract type CustomIndex <: Unsigned end
primitive type VertexIndex <: CustomIndex 16 end
primitive type EdgeIndex <: CustomIndex 16 end
primitive type FacetIndex <: CustomIndex 16 end
# A constructor to create values of the new type
VertexIndex(x::Number) = reinterpret(VertexIndex, UInt16(x) )
EdgeIndex(x::Number) = reinterpret(EdgeIndex, UInt16(x) )
FacetIndex(x::Number) = reinterpret(FacetIndex, UInt16(x) )
# A constructor to convert back.
UInt16(x::CustomIndex) = reinterpret(UInt16, x)
# This allows the REPL to show values of type MyInt8.
show(io::IO, x::CustomIndex) = print(io, UInt16(x))
ndigits0zpb( c::C, i::Integer ) where C<:CustomIndex = ndigits0zpb( UInt16(c), i )
Base.promote_rule( ::Type{C}, ::Type{I} ) where {C<:CustomIndex,I<:Integer} = C
convert( ::Type{C}, number::Number ) where {C<:CustomIndex} = reinterpret( C, convert( Base.UInt16, number ) )
So essentially CustomIndex
should just behave as UInt16
. I keep getting errors for missing methods like <
and so on and while I am ok with defining these, I feel like there is a better way of doing this.
So is there already an interface for things like that, that I am unaware of or is it even a good way to go?