# C union style for julia

**URL:** <https://discourse.julialang.org/t/c-union-style-for-julia/62149>\
**Category:** New to Julia\
**Created:** [May 31, 2021, 5:15pm UTC](https://discourse.julialang.org/t/c-union-style-for-julia/62149 "2021-05-31T17:15:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [May 31, 2021, 5:15pm UTC](https://discourse.julialang.org/t/c-union-style-for-julia/62149/1 "2021-05-31T17:15:58Z")

</div>

Hi,  
Is there something equivalent to C `union` in Julia?

Example:

```plaintext
union {
  double b;
  uint64_t u64;
} u;
u.b = d;
// use u.u64 here ....

```

I found the following package:

> **[GitHub - azurefx/CUnion.jl: C-like unions for Julia.](https://github.com/azurefx/CUnion.jl)**
>
> C-like unions for Julia. Contribute to azurefx/CUnion.jl development by creating an account on GitHub.

Is this the way the community would suggest to do it? Or is there a Julia base version which is recommended?

Thank you

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [May 31, 2021, 6:25pm UTC](https://discourse.julialang.org/t/c-union-style-for-julia/62149/2 "2021-05-31T18:25:13Z")

</div>

Not in the sense, that the memory size for this union is the same for all values allowed by the union definition (afaik). But there is Type-Unions:  
[https://docs.julialang.org/en/v1/manual/types/#Type-Unions](https://docs.julialang.org/en/v1/manual/types/#Type-Unions)

> [@gitboy16](#):
>
> Is this the way the community would suggest to do it? Or is there a Julia base version which is recommended?

To answer this, you need to tell us something about your problem/task/algorithm. There is no Julia way to do just the same as C union without a context.

CUnion.jl seems to implement the C union property like memory size is the largest type, so it seems to be it. But I don’t know if this is really needed.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [May 31, 2021, 6:52pm UTC](https://discourse.julialang.org/t/c-union-style-for-julia/62149/3 "2021-05-31T18:52:28Z")

</div>

No; you need to do something like

```julia
julia> mutable struct U
       u64::UInt64
       end
julia> Base.getproperty(u::U, s::Symbol) = s == :b ? reinterpret(Float64, getfield(u, :u64)) : getfield(u, s)
julia> Base.setproperty!(u::U, s::Symbol, b) = s == :b ? setproperty!(u, :u64, reinterpret(UInt64, b)) : setfield!(u, s, b) 
julia> u = U(1)
U(0x0000000000000001)
julia> u.b
5.0e-324
julia> u.b = -1.0
-1.0
julia> u 
U(0xbff0000000000000)

```

---

<div class="post-metadata">

**Author:** ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)\
**Post date:** [May 31, 2021, 7:36pm UTC](https://discourse.julialang.org/t/c-union-style-for-julia/62149/4 "2021-05-31T19:36:13Z")

</div>

Thank you. To give a bit of context, I am trying to build a faster `unique` function similar to this one:

> <https://github.com/2005m/kit/blob/master/src/dup.c#L424>

---

<div class="post-metadata">

**Author:** ![krrutkow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krrutkow/32/9138_2.png) [@krrutkow](https://discourse.julialang.org/u/krrutkow)\
**Post date:** [May 31, 2021, 7:42pm UTC](https://discourse.julialang.org/t/c-union-style-for-julia/62149/5 "2021-05-31T19:42:57Z")

</div>

You should have a look at [GitHub - analytech-solutions/CBinding.jl: Automatic C interfacing for Julia](https://github.com/analytech-solutions/CBinding.jl) for working with user-defined C types in Julia, especially if they are already defined in a header file.
