# Converting between type and immutable with the same layout

**URL:** <https://discourse.julialang.org/t/converting-between-type-and-immutable-with-the-same-layout/2784>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 20, 2017, 6:56pm UTC](https://discourse.julialang.org/t/converting-between-type-and-immutable-with-the-same-layout/2784 "2017-03-20T18:56:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [March 20, 2017, 6:56pm UTC](https://discourse.julialang.org/t/converting-between-type-and-immutable-with-the-same-layout/2784/1 "2017-03-20T18:56:47Z")

</div>

I want to convert a mutable type to an immutable type with the same layout, with the immutable type not having a constructor. Is the code below a safe way to do so?

```julia
type MutableFoo
  ptr::Ptr{Void}
end

immutable ImmutableFoo
  ptr::Ptr{Void}
end

a = MutableFoo(C_NULL)
b = unsafe_load(reinterpret(Ptr{ImmutableFoo}, (pointer_from_objref(a))))

```

---

<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:** [March 20, 2017, 7:22pm UTC](https://discourse.julialang.org/t/converting-between-type-and-immutable-with-the-same-layout/2784/2 "2017-03-20T19:22:59Z")

</div>

It’s possible (though shouldn’t happen in most cases) to have `a` free’d before the `unsafe_load`.

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [March 20, 2017, 8:00pm UTC](https://discourse.julialang.org/t/converting-between-type-and-immutable-with-the-same-layout/2784/3 "2017-03-20T20:00:11Z")

</div>

OK, so as long as `a` keeps referring to the original mutable it’s fine, right?

---

<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:** [March 20, 2017, 8:21pm UTC](https://discourse.julialang.org/t/converting-between-type-and-immutable-with-the-same-layout/2784/4 "2017-03-20T20:21:28Z")

</div>

`a` is the original mutable? I just mean that `unsafe_load(reinterpret(Ptr{ImmutableFoo}, (pointer_from_objref(a))))` does not guarantee that `a` will be valid when you read it.

`a` also doesn’t reference any other ojects. In fact, it can be bad if it does.

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [March 20, 2017, 8:33pm UTC](https://discourse.julialang.org/t/converting-between-type-and-immutable-with-the-same-layout/2784/5 "2017-03-20T20:33:53Z")

</div>

Yes, `a` is the original mutable, convention will be that this is the placeholder to prevent GC and it should be kept or the object (from C++ in this case) stored in the void pointer will be deleted by the finalizer attached to `a`. This is in fact an implementation detail for [referencing C++ objects](https://discourse.julialang.org/t/stack-allocating-wrapped-c-objects/2635). Since `MutableFoo` and `ImmutableFoo` are both created using the C API, they have no constructor attached (and I couldn’t find a way to attach one from C) so this hack allows me to convert to the stack-allocated “reference type” without using `ccall`.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [March 22, 2017, 10:22pm UTC](https://discourse.julialang.org/t/converting-between-type-and-immutable-with-the-same-layout/2784/6 "2017-03-22T22:22:24Z")

</div>

A simpler, more common solution is to define that  
`MutableFoo = Ref{ImmutableFoo}`
