10
votes

The Julia documentation says:

A primitive type is a concrete type whose data consists of plain old bits. Classic examples of primitive types are integers and floating-point values. Unlike most languages, Julia lets you declare your own primitive types, rather than providing only a fixed set of built-in ones. In fact, the standard primitive types are all defined in the language itself:

I'm unable to find an example of how to do this, though, either in the docs or in the source code or anywhere else. What I'm looking for is an example of how to declare a primitive type, and how to subsequently implement a function or method on that type that works by manipulating those bits.

Is anyone able to point me towards an example? Thanks.


Edit: It's clear how to declare a primitive type, as there are examples immediately below the above quote in the doc. I'm hoping for information about how to subsequently manipulate them. For example, say I wanted to (pointlessly) implement my own primitive type MyInt8. I could declare that with primitive type MyInt8 <: Signed 8 end. But how would I subsequently implement a function myplus that manipulated the bits within Myint8?

PS in case it helps, the reason I'm asking is not because I need to do anything specific in Julia; I'm designing my own language for fun, and am researching how other languages implement various things.

1
There are examples of declaration right below that quote. Before v0.6, they were called (and declared as) bitstypes. But yeah, there's not much more explanation than how to declare them...phipsgabler
@phg Yes - thanks, it's clear how to declare them. I was looking for more information on how to subsequently manipulate them. I'll edit the question to make that clearer.Sam Roberts
If you are asking for operator overloading for your new Myint8 type, you can look at this question (made just of yesterday): stackoverflow.com/questions/47422089/…Antonello
@Antonello The type referred to in that question isn't a primitive type. It implements + by manipulating the fields of the type Foo - I'm interested in doing a similar thing, but with a primitive type, and manipulating the bits of that type.Sam Roberts
Maybe "Directly manipulating bits for a custom primitive type in Julia" would be a better title :)Alexander Morley

1 Answers

11
votes
# Declare the new type.
primitive type MyInt8 <: Signed 8 end

# A constructor to create values of the type MyInt8.
MyInt8(x :: Int8) = reinterpret(MyInt8, x)

# A constructor to convert back.
Int8(x :: MyInt8) = reinterpret(Int8, x)

# This allows the REPL to show values of type MyInt8.
Base.show(io :: IO, x :: MyInt8) = print(io, Int8(x))

# Declare an operator for the new type.
import Base: +

+ (a :: MyInt8, b :: MyInt8) = MyInt8(Int8(a) + Int8(b))

The key function here is reinterpret. It allows the bit representation of an Int8 to be treated as the new type.

To store a value with a custom bit layout, inside the MyInt8 constructor, you could perform any of the standard bit manipulation functions on the Int8 before 'reinterpreting' them as a MyInt8.