# Trouble defining hash/isequal for an interval type

**URL:** https://discourse.julialang.org/t/trouble-defining-hash-isequal-for-an-interval-type/106629
**Category:** General Usage
**Tags:** question, dictionary, hash
**Created:** [November 23, 2023, 9:01am UTC](https://discourse.julialang.org/t/trouble-defining-hash-isequal-for-an-interval-type/106629 "2023-11-23T09:01:31Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [November 26, 2023, 8:45am UTC](https://discourse.julialang.org/t/trouble-defining-hash-isequal-for-an-interval-type/106629/7 "2023-11-26T08:45:23Z")

</div>

No, of course. In the way I had defined the relation is not of equivalence as it has no transitive property (as pointed out by @stevengj).  
I just tried to explain (I use Google Translate to write in English :)) what I had improperly tried to do.  
I finally realized that I couldn’t do [what I wanted](https://discourse.julialang.org/t/how-to-separate-joint-and-disjoint-intervals-from-an-interval-vector/106515) using the Dict structure.

Below is an example, following your instructions

```julia
julia> using IntervalArithmetic

julia> import Base: hash,==, isequal

julia> hash(a::Interval) = hash(a.lo-a.hi)
hash (generic function with 102 methods)

julia> ==(a::Interval, b::Interval) = a.lo-a.hi == b.lo - b.hi
== (generic function with 188 methods)

julia> isequal(a::Interval, b::Interval) = isequal(a.lo-a.hi, b.lo - b.hi)
isequal (generic function with 29 methods)

julia> a=Interval(0,2)
[0, 2]

julia> b=Interval(2,4)
[2, 4]

julia> c=Interval(-3,-1)
[-3, -1]

julia> da=Dict(a=>"a")
Dict{Interval{Float64}, String} with 1 entry:
  [0, 2] => "a"

julia> dc=Dict(c=>'c')
Dict{Interval{Float64}, Char} with 1 entry:
  [-3, -1] => 'c'

julia> db=Dict(b=>'b')
Dict{Interval{Float64}, Char} with 1 entry:
  [2, 4] => 'b'
julia> mergewith((x...)->join(x,'-'), da,db,dc)        
Dict{Interval{Float64}, Any} with 1 entry:
  [0, 2] => "a-b-c"

```

---

_[View the full topic](https://discourse.julialang.org/t/trouble-defining-hash-isequal-for-an-interval-type/106629)._
