Convincing Julia that 2 types are the same

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.

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.

4 Likes

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

1 Like

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

1 Like

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

const MyType = Union{Type1,Type2}

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

2 Likes