# User-defined primitive types -- how to access?

**URL:** https://discourse.julialang.org/t/user-defined-primitive-types-how-to-access/6745
**Category:** General Usage
**Created:** [October 28, 2017, 4:49pm UTC](https://discourse.julialang.org/t/user-defined-primitive-types-how-to-access/6745 "2017-10-28T16:49:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![arch.d.robison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arch.d.robison/32/17699_2.png) [@arch.d.robison](https://discourse.julialang.org/u/arch.d.robison)
#### Post date: [October 28, 2017, 4:49pm UTC](https://discourse.julialang.org/t/user-defined-primitive-types-how-to-access/6745/1 "2017-10-28T16:49:48Z")

</div>

The [documentation](https://docs.julialang.org/en/stable/manual/types/#Primitive-Types-1) explains how to declare a primitive type, but seems silent on how to set the bits or read them.

For example, given:

```julia
primitive type Foo 8 end

```

how would I construct a Foo holding 42, or given a Foo, how do I read the bits?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 28, 2017, 5:37pm UTC](https://discourse.julialang.org/t/user-defined-primitive-types-how-to-access/6745/2 "2017-10-28T17:37:49Z")

</div>

`reinterpret`

---

<div class="post-metadata">

### Author: ![bicycle1885](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bicycle1885/32/107_2.png) [@bicycle1885](https://discourse.julialang.org/u/bicycle1885)
#### Post date: [October 28, 2017, 6:22pm UTC](https://discourse.julialang.org/t/user-defined-primitive-types-how-to-access/6745/3 "2017-10-28T18:22:30Z")

</div>

A real-world example may be helpful: [https://github.com/BioJulia/BioSymbols.jl/blob/9680751fabdb26ddc7a841c2115c149ec78e7617/src/nucleicacid.jl#L29-L47](https://github.com/BioJulia/BioSymbols.jl/blob/9680751fabdb26ddc7a841c2115c149ec78e7617/src/nucleicacid.jl#L29-L47).

```julia
"""
An abstract nucleic acid type.
"""
@compat abstract type NucleicAcid end

"""
A deoxyribonucleic acid type.
"""
@compat primitive type DNA <: NucleicAcid 8 end

"""
A ribonucleic acid type.
"""
@compat primitive type RNA <: NucleicAcid 8 end

# Conversion from/to integers
# ---------------------------

Base.convert{T<:NucleicAcid}(::Type{T}, nt::UInt8) = reinterpret(T, nt)
Base.convert{T<:NucleicAcid}(::Type{UInt8}, nt::T) = reinterpret(UInt8, nt)
Base.convert{T<:Number,S<:NucleicAcid}(::Type{T}, nt::S) = convert(T, UInt8(nt))
Base.convert{T<:Number,S<:NucleicAcid}(::Type{S}, nt::T) = convert(S, UInt8(nt))

```
