# In Set, what's decompose?

**URL:** <https://discourse.julialang.org/t/in-set-whats-decompose/124031>\
**Category:** General Usage\
**Tags:** hash\
**Created:** [December 20, 2024, 4:46pm UTC](https://discourse.julialang.org/t/in-set-whats-decompose/124031 "2024-12-20T16:46:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)\
**Post date:** [December 20, 2024, 4:46pm UTC](https://discourse.julialang.org/t/in-set-whats-decompose/124031/1 "2024-12-20T16:46:44Z")

</div>

I wrote a function which computes something using a `SortedSet` of a type I defined, which is a fixed-size `SVector` with no variable-size fields. I thought that maybe using a `Set` would make it faster, so I changed `SortedSet` to `Set` and ran the function. It threw a `MethodError` trying to call `decompose` on my type. The stacktrace indicates that `decompose` is called from `hash`. How should I define `decompose` on my type, or should I define `hash`?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 20, 2024, 5:25pm UTC](https://discourse.julialang.org/t/in-set-whats-decompose/124031/2 "2024-12-20T17:25:16Z")

</div>

`decompose` is an implementation detail internal to `Base`. Define equality and hashing for your type.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [December 20, 2024, 5:51pm UTC](https://discourse.julialang.org/t/in-set-whats-decompose/124031/3 "2024-12-20T17:51:46Z")

</div>

I believe you’d only hit this if your type subsets `Number` (or some subtype thereof). Note that it can be tricky to extend numeric types — you can often find yourself deep in internals like these pretty quickly.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 20, 2024, 6:02pm UTC](https://discourse.julialang.org/t/in-set-whats-decompose/124031/4 "2024-12-20T18:02:14Z")

</div>

Also, equality and hashing must be consistent. In particular this holds even when comparing values of two different types. This basically means that `hash` for custom `Number` types needs to be implemented by converting to a `Number` type defined by `Base` (or `Core`), and then hashing that. E.g., if `YourType(7) == 7`, then `hash(YourType(7), h) === hash(7, h)` must hold too, for any `h::UInt`.

---

<div class="post-metadata">

**Author:** ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)\
**Post date:** [December 22, 2024, 6:26am UTC](https://discourse.julialang.org/t/in-set-whats-decompose/124031/5 "2024-12-22T06:26:38Z")

</div>

It is a subtype of Real, and can be multiplied and divided, but it’s not exported, `==` is not defined between this type and anything else, and it’s never used in a `Union`. So I defined a hash with no relation to the hash of floats and integers, and the algorithm runs three times as fast as it did with `SortedSet`.
