# "isomorphic" datastructures

**URL:** https://discourse.julialang.org/t/isomorphic-datastructures/136733
**Category:** General Usage
**Created:** [April 16, 2026, 9:00am UTC](https://discourse.julialang.org/t/isomorphic-datastructures/136733 "2026-04-16T09:00:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)
#### Post date: [April 16, 2026, 9:00am UTC](https://discourse.julialang.org/t/isomorphic-datastructures/136733/1 "2026-04-16T09:00:22Z")

</div>

Hi,

Here is a X/Y problem

**Problem X** :  
I handle data structures which are a nesting of Tuples, NamedTuples and SVector, of various `Real`s.

I want to create a function `isomorph(a,b)` that is equivalent to `typeof(a)==typeof(b)` _except_ that it must also return `true` if the concrete `Real` types are different. For example a `NTuple{3,Float64}` and a `NTuple{3,Int64}` are, I say, isomorph.

I want to implement it in the form of a set of methods, recursing down the datastructure.

**Problem Y**  
I decide to create methods that operate on `DataType`s, like `isomorph(typeof(a),typeof(b))` instead of variables because

1. otherwise I do not sea how to compare two `SVector`s of length 0.
2. this emphasizes the intention of having this code run at compile time

I start with

```julia-auto
isomorph(::Type{<:Any } ,::Type{<:Any }) = false # in doubt, no
isomorph(::Type{<:Real} ,::Type{<:Real}) = true

```

So the method for `SVector` is simply

```julia-auto
isomorph(::Type{<:SArray{S,Ta}},::Type{<:SArray{S,Tb}}) where{S,Ta,Tb} = 
                     isomorph(Ta,Tb)

```

For `Tuples`, I use a hack (ugh! Julia internal) to get the types of the `Tuple` components: `Ta.types`, as in

```julia-auto
isomorph(Ta::Type{<:Tuple},Tb::Type{<:Tuple}) = length(Ta.types) == length(Tb.types) && all(isomorph.(Ta.types,Tb.types))

```

The same `Ta.types` works for `NameTuples`, but **how to I get the `keys` from the type**?

🙂

---

<div class="post-metadata">

### Author: ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)
#### Post date: [April 16, 2026, 9:06am UTC](https://discourse.julialang.org/t/isomorphic-datastructures/136733/2 "2026-04-16T09:06:17Z")

</div>

`fieldnames(Ta)` 😁

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [April 16, 2026, 11:54am UTC](https://discourse.julialang.org/t/isomorphic-datastructures/136733/3 "2026-04-16T11:54:07Z")

</div>

> [@Philippe\_Maincon1](#):
>
> For `Tuples`, I use a hack (ugh! Julia internal) to get the types of the `Tuple` components: `Ta.types`,

You can say

```julia-auto
julia> fieldtypes(Tuple{Bool,Char})
(Bool, Char)

```

---

<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: [April 16, 2026, 3:34pm UTC](https://discourse.julialang.org/t/isomorphic-datastructures/136733/4 "2026-04-16T15:34:47Z")

</div>

> [@Philippe\_Maincon1](#):
>
> Here is a X/Y problem

You realize that [“XY problem”](https://xyproblem.info/) refers to a question where you have mis-identified the root problem, right?

> I handle data structures which are a nesting of Tuples, NamedTuples and SVector, of various `Real`s. I want to create a function `isomorph(a,b)` that is equivalent to `typeof(a)==typeof(b)` _except_ that it must also return `true` if the concrete `Real` types are different.

What is the underlying problem that you are trying to solve that requires this?

---

<div class="post-metadata">

### Author: ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)
#### Post date: [April 17, 2026, 4:44am UTC](https://discourse.julialang.org/t/isomorphic-datastructures/136733/5 "2026-04-17T04:44:13Z")

</div>

Ah great. And that’s API-julia, not a hack. Thanks!

---

<div class="post-metadata">

### Author: ![Philippe\_Maincon1](https://avatars.discourse-cdn.com/v4/letter/p/ec9cab/32.png) [@Philippe\_Maincon1](https://discourse.julialang.org/u/Philippe_Maincon1)
#### Post date: [April 17, 2026, 5:17am UTC](https://discourse.julialang.org/t/isomorphic-datastructures/136733/6 "2026-04-17T05:17:35Z")

</div>

Hm, so I stretched the use XY. I wanted to express that I decided to solve X by solving subproblem Y, but… maybe there is a way to attack X that does not require solving Y.

And then you zoom you again asking why solve X? 😃 . Let my try to answer.

**General answer:**  
I write code that operates on the stack (se below about the specific context pushing in this direction), so I have various data structures that nest `SVector`s, `Tuple`s and `NamedTuple`s, with some concrete `Real`s at the bottom.

I create functions that can do specific operation on the individual `Real`s, for arbitrary data structures. This includes binary operations (between two structures), requiring me to check if they are `isomorph`.

**Specific context**  
I develop [Muscade.jl](https://github.com/SINTEF/Muscade.jl) which relies heavily on forward automatic differentiation. Going beyond what [ForwardDiff.jl](https://juliadiff.org/ForwardDiff.jl) can do today, in file [Taylor.jl](https://github.com/SINTEF/Muscade.jl/blob/main/src/Taylor.jl) I create functionality to speed up automatic differentiation by differentiating subfunctions separately and apply the differentiation chain rule… to all sorts of nested structure (there we have it).

The performance gain has been huge in my specific application (see the calls to `apply` in [BeamElement.jl](https://github.com/SINTEF/Muscade.jl/blob/main/toolbox/BeamElement.jl)).

If anyone wants understand [Taylor.jl](https://github.com/SINTEF/Muscade.jl/blob/main/src/Taylor.jl) or talk about bringing this to [ForwardDiff.jl](https://juliadiff.org/ForwardDiff.jl) , I’m happy to share and help.
