# How to convert user-defined struct{T} to struct{Any} without redundant copy?

**URL:** https://discourse.julialang.org/t/how-to-convert-user-defined-struct-t-to-struct-any-without-redundant-copy/13450
**Category:** General Usage
**Tags:** syntax
**Created:** [August 14, 2018, 4:27pm UTC](https://discourse.julialang.org/t/how-to-convert-user-defined-struct-t-to-struct-any-without-redundant-copy/13450 "2018-08-14T16:27:47Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)
#### Post date: [August 14, 2018, 4:27pm UTC](https://discourse.julialang.org/t/how-to-convert-user-defined-struct-t-to-struct-any-without-redundant-copy/13450/1 "2018-08-14T16:27:48Z")

</div>

```julia
x = Int[1]
isimmuatble(x) # => false, so `===` performs comparing by reference, which checks if lhs and rsh are same.
x === convert(Vector{Any}, x) # => true 

```

What if I want to do such stuffs on custom structure

```julia
struct S{T} 
     a :: T # to avoid singleton, we need at least one field.
end

s = S{Int}(1)
convert(S{Any}, s) 

```

---

<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: [August 14, 2018, 4:50pm UTC](https://discourse.julialang.org/t/how-to-convert-user-defined-struct-t-to-struct-any-without-redundant-copy/13450/2 "2018-08-14T16:50:01Z")

</div>

Which version are you using? That is impossible in any versions for any types AFAICT.

---

<div class="post-metadata">

### Author: ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)
#### Post date: [August 14, 2018, 4:51pm UTC](https://discourse.julialang.org/t/how-to-convert-user-defined-struct-t-to-struct-any-without-redundant-copy/13450/3 "2018-08-14T16:51:07Z")

</div>

I’m sorry. Exactly I’m using v1.0.0.

---

<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: [August 14, 2018, 5:08pm UTC](https://discourse.julialang.org/t/how-to-convert-user-defined-struct-t-to-struct-any-without-redundant-copy/13450/4 "2018-08-14T17:08:33Z")

</div>

Still, that’s impossible in any version on any types. The code you have (corrected for typo) doesn’t do what you claim it does, assuming you didn’t define any local names to change the meaning for any of the operations.

```julia
julia> x = Int[1]
1-element Array{Int64,1}:
 1

julia> isimmutable(x) # => false, so `===` performs comparing by reference, which checks if lhs and rsh are same.
false

julia> x === convert(Vector{Any}, x) # => true
false

```

---

<div class="post-metadata">

### Author: ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)
#### Post date: [August 14, 2018, 5:16pm UTC](https://discourse.julialang.org/t/how-to-convert-user-defined-struct-t-to-struct-any-without-redundant-copy/13450/5 "2018-08-14T17:16:39Z")

</div>

@yuyichao Yes, I’ve just checked my julia history and found that I typed `==` instead of `===`.

However are there anything like `reinterpret_cast`?

---

<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: [August 14, 2018, 5:27pm UTC](https://discourse.julialang.org/t/how-to-convert-user-defined-struct-t-to-struct-any-without-redundant-copy/13450/6 "2018-08-14T17:27:34Z")

</div>

No. Julia is strongly typed and this is fundamentally impossible.

---

<div class="post-metadata">

### Author: ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)
#### Post date: [August 15, 2018, 5:03am UTC](https://discourse.julialang.org/t/how-to-convert-user-defined-struct-t-to-struct-any-without-redundant-copy/13450/7 "2018-08-15T05:03:04Z")

</div>

> [@yuyichao](#):
>
> No. Julia is strongly typed and this is fundamentally impossible.

Is reinterpret\_cast really fundamentally impossible for strong typed languages?

---

<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: [August 15, 2018, 12:32pm UTC](https://discourse.julialang.org/t/how-to-convert-user-defined-struct-t-to-struct-any-without-redundant-copy/13450/8 "2018-08-15T12:32:43Z")

</div>

Depending on what you mean.  
It’s impossible to cast a object to be treated as a different type. You can, however, cast between pointers in julia. In fact, every cast in C and C++ are still possible in julia, it’s just that pointers do not represent object.
