# Convincing Julia that 2 types are the same

**URL:** https://discourse.julialang.org/t/convincing-julia-that-2-types-are-the-same/30863
**Category:** General Usage
**Created:** [November 8, 2019, 3:40pm UTC](https://discourse.julialang.org/t/convincing-julia-that-2-types-are-the-same/30863 "2019-11-08T15:40:37Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [November 8, 2019, 3:40pm UTC](https://discourse.julialang.org/t/convincing-julia-that-2-types-are-the-same/30863/1 "2019-11-08T15:40:37Z")

</div>

Let’s say I have structs A and B. They are defined the same way, but weren’t specified to be part of a common abstract type.

Is there a way to tell Julia that they’re really the same type, and that functions that accept A should accept B?

This is relevant to e.g. loading data from a JLD2 file where the type is reconstructed but was defined in a different module and so is considered to be different.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [November 8, 2019, 3:49pm UTC](https://discourse.julialang.org/t/convincing-julia-that-2-types-are-the-same/30863/3 "2019-11-08T15:49:21Z")

</div>

No, the type hierarchy is defined at type definition, so it cannot be altered later.

When loading JLDs you need to make sure that the types are defined already, so it does not need to reconstruct them.

You maybe able avoid this problem if you can get away with duck-typed functions.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [November 9, 2019, 1:24am UTC](https://discourse.julialang.org/t/convincing-julia-that-2-types-are-the-same/30863/4 "2019-11-09T01:24:39Z")

</div>

You could reinterpret one as the other. You could also make a convert method with this reinterpret code. It’s not exactly what you’re asking for, though

---

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [November 9, 2019, 3:58am UTC](https://discourse.julialang.org/t/convincing-julia-that-2-types-are-the-same/30863/5 "2019-11-09T03:58:43Z")

</div>

Ah, that looks like it would work though. Easy enough to compose. Thanks!

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [November 9, 2019, 2:04pm UTC](https://discourse.julialang.org/t/convincing-julia-that-2-types-are-the-same/30863/6 "2019-11-09T14:04:17Z")

</div>

> [@BioTurboNick](#):
>
> Is there a way to tell Julia that they’re really the same type, and that functions that accept A should accept B?

Just use a `Union` whenever you want a function to accept both types. You can even define

```julia
const MyType = Union{Type1,Type2}

```

and then use `MyType` where you want to accept both types.
