# Let struct instance equal each other just depending on their struct name

**URL:** <https://discourse.julialang.org/t/let-struct-instance-equal-each-other-just-depending-on-their-struct-name/84254>\
**Category:** New to Julia\
**Created:** [July 15, 2022, 5:47am UTC](https://discourse.julialang.org/t/let-struct-instance-equal-each-other-just-depending-on-their-struct-name/84254 "2022-07-15T05:47:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [July 15, 2022, 5:47am UTC](https://discourse.julialang.org/t/let-struct-instance-equal-each-other-just-depending-on-their-struct-name/84254/1 "2022-07-15T05:47:41Z")

</div>

I have a `abstract ttt`, which owe two subtypes `Tec1,Tec2` :

```julia
abstract type ttt end
struct Tec1{T} <: ttt
    n::T
end
struct Tec2{T} <: ttt
    n::T
end

```

I make the instances genrerated from the two `struct` equal each other just depending on their struct name:

```julia
Base.hash(x::T,h::UInt) where {T <: ttt}=hash(T.name,hash(0x7d6979235cb005d0, h))
Base.:(==)(x::T,y::N) where {T <: ttt, N <: ttt}=T.name == N.name

```

So:

```julia
Tec1(10) == Tec1(100)
true

Tec1(10) == Tec2(100)
false

haskey(
    Dict(
        Tec1(10) => 100
        ),
    Tec1(100)
    )
true

```

But, when I make another struct wrapping the two struct

```julia
struct Tel{T}
    n::T
end

```

There happend:

```julia
Tel(Tec1(10)) == Tel(Tec1(100))

false

```

In my view, `Tuple` isa just a `unmutable struct`, and `(1,Tec1(10)) == (1,Tec(100))` is `true`, the why `Tel(Tec1(10)) == Tel(Tec1(100))` is `false`?

So, what should I do to make this happen:

```julia
Tel(Tec1(10)) == Tel(Tec1(100))
true

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 15, 2022, 6:41am UTC](https://discourse.julialang.org/t/let-struct-instance-equal-each-other-just-depending-on-their-struct-name/84254/2 "2022-07-15T06:41:21Z")

</div>

You haven’t defined an equality method for `Tel`, so afaik, it falls back to `===`.  
BTW it isn’t necessary to access the name of the struct `Tec1`. Just use dispatch:

```julia
Base.:(==)(::T, ::T) where {T<:ttt} = true

```

Also, struct names should be capitalized: `Ttt`, not `ttt`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 15, 2022, 6:46am UTC](https://discourse.julialang.org/t/let-struct-instance-equal-each-other-just-depending-on-their-struct-name/84254/3 "2022-07-15T06:46:43Z")

</div>

> [@Brian1](#):
>
> `T.name`

Where does the field `name` come from? It doesn’t appear to be defined:

> [@Brian1](#):
>
> ```julia
> struct Tec1{T} <: ttt
> n::T
> end
> 
> ```

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [July 15, 2022, 6:50am UTC](https://discourse.julialang.org/t/let-struct-instance-equal-each-other-just-depending-on-their-struct-name/84254/4 "2022-07-15T06:50:24Z")

</div>

The field name dosen’t come from the instance of `Tec1`, it comes from `typeof(tec1)` or `T`  
It can be accessed in this way :

```julia
test1(x::T) where T=begin
    println(T.name)
end

-> test1(10)
typename(Int64)

```

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [July 15, 2022, 7:06am UTC](https://discourse.julialang.org/t/let-struct-instance-equal-each-other-just-depending-on-their-struct-name/84254/5 "2022-07-15T07:06:16Z")

</div>

Thanks, this style is much more cleaner than the original one.

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [July 15, 2022, 7:13am UTC](https://discourse.julialang.org/t/let-struct-instance-equal-each-other-just-depending-on-their-struct-name/84254/6 "2022-07-15T07:13:14Z")

</div>

```julia
Tel(Tec1(10)) == Tel(Tec1(100))

```

This falls back to `===` : `Tel(Tec1(10)) === Tel(Tec1(100))`, then this will check `Tel(Tec1(10)).n === Tel(Tec1(100)).n`, while `Tec1(10) === Tec1(100) ` isa `false`.  
Is there a way I can compel that :

```julia
Tec1(10) === Tec2(100)
true

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 15, 2022, 8:00am UTC](https://discourse.julialang.org/t/let-struct-instance-equal-each-other-just-depending-on-their-struct-name/84254/7 "2022-07-15T08:00:00Z")

</div>

No, I don’t think so. And you definitely shouldn’t. `x === y` means that there is no way to distinguish between `x` and `y`. But in your case there clearly is, since `x.n` and `y.n` are different.
