# Custom hash not called for field in another struct – expected?

**URL:** https://discourse.julialang.org/t/custom-hash-not-called-for-field-in-another-struct-expected/130869
**Category:** General Usage
**Tags:** question
**Created:** [July 19, 2025, 2:21pm UTC](https://discourse.julialang.org/t/custom-hash-not-called-for-field-in-another-struct-expected/130869 "2025-07-19T14:21:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)
#### Post date: [July 19, 2025, 2:21pm UTC](https://discourse.julialang.org/t/custom-hash-not-called-for-field-in-another-struct-expected/130869/1 "2025-07-19T14:21:40Z")

</div>

The Julia docs say:

> [@](#):
>
> New types should implement the 2-argument form, typically by calling the 2-argument hash method recursively in order to mix hashes of the contents with each other (and with `h`).

But when I do this, it seems to ignore my custom hash function in certain contexts:

```julia-repl
julia> struct Foo
       end

julia> Base.hash(::Foo, h::UInt) = error()

```

The following calls are what I would expect:

```julia-repl
julia> hash(Foo())
ERROR:
Stacktrace:
 [1] error()
   @ Base ./error.jl:44
 [2] hash(::Foo, h::UInt64)
   @ Main ./REPL[2]:1

julia> hash((Foo(),))
ERROR:
Stacktrace:
 [1] error()
   @ Base ./error.jl:44
 [2] hash(::Foo, h::UInt64)
   @ Main ./REPL[2]:1

```

But this one is not expected:

```julia
julia> struct Bar
           x::Foo
       end

julia> hash(Bar(Foo()))
0xb4bfb67e8815404c

```

Is there a reason why the default `hash` on structs does not recursively travel through fields? Does this mean if I implement a custom hashing function for my type, I also need to overload hash for any type that takes it as a property to ensure my hash gets hit?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [July 19, 2025, 2:24pm UTC](https://discourse.julialang.org/t/custom-hash-not-called-for-field-in-another-struct-expected/130869/2 "2025-07-19T14:24:52Z")

</div>

The default hash is `objectid` (matching the default `==` which is `===`) which doesn’t look at the fields.

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [July 19, 2025, 2:26pm UTC](https://discourse.julialang.org/t/custom-hash-not-called-for-field-in-another-struct-expected/130869/3 "2025-07-19T14:26:26Z")

</div>

see also: [Convenient ways to define == and hash · Issue #53565 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/53565)

---

<div class="post-metadata">

### Author: ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)
#### Post date: [July 19, 2025, 2:54pm UTC](https://discourse.julialang.org/t/custom-hash-not-called-for-field-in-another-struct-expected/130869/4 "2025-07-19T14:54:59Z")

</div>

Thanks!

Maybe it’s just me but the default `==` and `hash` feel kinda unintuitive, no? I know it won’t change, just wondering why my mental model is broken

```julia-repl
julia> mutable struct A
           a::Float64
       end

julia> x = A(1.0)
A(1.0)

julia> hash(deepcopy(x))
0x9e99cc3918a450f2

julia> hash(x)
0x3f7020c38a5b2e12

julia> deepcopy(x) == x
false

```

I guess I just never think of a “hash” as being sensitive to the particular location in memory.

Obviously this doesn’t matter for immutables but still

---

<div class="post-metadata">

### Author: ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)
#### Post date: [July 19, 2025, 2:56pm UTC](https://discourse.julialang.org/t/custom-hash-not-called-for-field-in-another-struct-expected/130869/5 "2025-07-19T14:56:16Z")

</div>

Ah, there’s even an issue for it. Good to know it’s not just me.

> <https://github.com/JuliaLang/julia/issues/4648>
>
> This doesn't make much sense:
> 
> \`\`\` julia
> julia\> immutable Foo{T}
> bar::T…
> end
> 
> julia\> Foo("baz") == Foo("baz")
> false
> 
> julia\> Foo("baz").bar == Foo("baz").bar
> true
> 
> julia\> Foo(1) == Foo(1)
> true
> \`\`\`
> 
> If the fields are \`==\` to each other then the objects should be \`==\` to each other.
