# Casting struct into an apparently identical type

**URL:** https://discourse.julialang.org/t/casting-struct-into-an-apparently-identical-type/108980
**Category:** General Usage
**Created:** [January 18, 2024, 6:10pm UTC](https://discourse.julialang.org/t/casting-struct-into-an-apparently-identical-type/108980 "2024-01-18T18:10:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)
#### Post date: [January 18, 2024, 6:10pm UTC](https://discourse.julialang.org/t/casting-struct-into-an-apparently-identical-type/108980/1 "2024-01-18T18:10:07Z")

</div>

Is it possible (even if hacky and not advisable) to do something like the following? Forewarning: this is a contrived example that just happens to work as an MWE exercise for my use case.

Let `Foo` be some type whose definition I can’t control and with a fundamentally-incompatible inner constructor.

```julia
struct Foo{T}
    x::T
    Foo(x) = error()
end

```

Is it possible to declare a shadow implementation a la

```julia
struct _ShadowFoo{T}
    x::T
end

```

and then cast an instance of this type to the type being shadowed?

```julia
reinterpret(Foo{Float64}, _ShadowFoo(1.0))

```

This use of `reinterpret` doesn’t seem to work for me and I haven’t been able to find a different function that achieves this effect.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [January 18, 2024, 6:50pm UTC](https://discourse.julialang.org/t/casting-struct-into-an-apparently-identical-type/108980/2 "2024-01-18T18:50:40Z")

</div>

Reinterpret usually works on mutable arrays and may be implemented for some other mutable types.

We can accomplish what you want as follows.

```julia
julia> shadows = [_ShadowFoo(1.0)]
1-element Vector{_ShadowFoo{Float64}}:
 _ShadowFoo{Float64}(1.0)

julia> reinterpret(Foo{Float64}, shadows)[1]
Foo{Float64}(1.0)

```

Here we managed to construct `Foo` despite having no constructor.

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [January 18, 2024, 6:55pm UTC](https://discourse.julialang.org/t/casting-struct-into-an-apparently-identical-type/108980/3 "2024-01-18T18:55:23Z")

</div>

I think v1.10 added support for reinterpreting between `isbitstype` types. On v1.10, the original post’s code all works for me, concluding with

```julia-repl
julia> reinterpret(Foo{Float64}, _ShadowFoo(1.0))
Foo{Float64}(1.0)

```

I think it mostly uses the procedure that **mkitti** suggests, but it’s builtin and maybe does it in a way that is slightly more sophisticated and possibly more performant. See `@less Base._reinterpret(ComplexF32, 1.0)` for the source.
