# Minimal set of methods for integer primitive type

**URL:** <https://discourse.julialang.org/t/minimal-set-of-methods-for-integer-primitive-type/14540>\
**Category:** General Usage\
**Created:** [September 4, 2018, 2:40pm UTC](https://discourse.julialang.org/t/minimal-set-of-methods-for-integer-primitive-type/14540 "2018-09-04T14:40:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [September 4, 2018, 2:40pm UTC](https://discourse.julialang.org/t/minimal-set-of-methods-for-integer-primitive-type/14540/1 "2018-09-04T14:40:03Z")

</div>

I am trying to have custom Integer-types I can use for dispatch on `getindex`.

So far I tried the following:

```julia
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?

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [September 5, 2018, 12:56pm UTC](https://discourse.julialang.org/t/minimal-set-of-methods-for-integer-primitive-type/14540/2 "2018-09-05T12:56:41Z")

</div>

Is there something like a type alias?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 5, 2018, 1:10pm UTC](https://discourse.julialang.org/t/minimal-set-of-methods-for-integer-primitive-type/14540/3 "2018-09-05T13:10:01Z")

</div>

No. People use macros to make the definitions more compact, eg see the [code of SaferIntegers.jl](https://github.com/JeffreySarnoff/SaferIntegers.jl/blob/master/src/arith_ops.jl).

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [September 18, 2018, 3:07pm UTC](https://discourse.julialang.org/t/minimal-set-of-methods-for-integer-primitive-type/14540/4 "2018-09-18T15:07:25Z")

</div>

You can look at the registered `BitIntegers` package.

```julia
julia> import BitIntegers

julia> abstract type CustomIndex <: BitIntegers.AbstractBitUnsigned end

julia> primitive type VertexIndex <: CustomIndex 16 end

julia> VertexIndex(3) * VertexIndex(0x5)
0x000f

```

Note that `BitIntegers.AbstractBitUnsigned` is not public API and may change.
