# Struct vs primitive type?

**URL:** <https://discourse.julialang.org/t/struct-vs-primitive-type/19422>\
**Category:** General Usage\
**Created:** [January 9, 2019, 12:11pm UTC](https://discourse.julialang.org/t/struct-vs-primitive-type/19422 "2019-01-09T12:11:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [January 9, 2019, 12:11pm UTC](https://discourse.julialang.org/t/struct-vs-primitive-type/19422/1 "2019-01-09T12:11:18Z")

</div>

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:

```julia
struct Latch
    data::UInt32
end

```

```julia
primitive type Latch 32 end

```

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

---

<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:** [January 9, 2019, 12:31pm UTC](https://discourse.julialang.org/t/struct-vs-primitive-type/19422/2 "2019-01-09T12:31:33Z")

</div>

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.
