Struct vs primitive type?

When should I use primitive types over structs and why?

Here is a concrete example:
I am working with a C code and there is a UInt32 called LATCH that is interpreted in fancy ways. E.g. so me of its bits are flags and other bits encode two very small integers.
I need to interface with this C code and want an analogous julia type with same memory layout. There are to options:

struct Latch
    data::UInt32
end
primitive type Latch 32 end

Which one should I choose? What are the advantages/disadvantages?

If you are just working on the type in julia code, it doesn’t matter. All operations are most easily done on the UInt32 and it’s the difference between using reinterpret and field access.

If you are passing this to/from C, then the argument and return type must be primitive type (assuming it’s a uint32_t in C). Using a primitive type is easier in this case though there’s nothing wrong with passing a UInt32 to C and convert that to your struct in julia either.

3 Likes