# Equality check compatible with custom \`equals()\` methods

**URL:** <https://community.haxe.org/t/equality-check-compatible-with-custom-equals-methods/4493>\
**Category:** Haxe\
**Created:** [December 23, 2024, 5:29pm UTC](https://community.haxe.org/t/equality-check-compatible-with-custom-equals-methods/4493 "2024-12-23T17:29:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![neillrobson](https://community.haxe.org/user_avatar/community.haxe.org/neillrobson/32/2413_2.png) [@neillrobson](https://community.haxe.org/u/neillrobson)\
**Post date:** [December 23, 2024, 5:29pm UTC](https://community.haxe.org/t/equality-check-compatible-with-custom-equals-methods/4493/1 "2024-12-23T17:29:28Z")

</div>

I’ve got an abstract class with the following method declaration:

```haxe
abstract function solveProblem(data:String):Any;

```

I’d like to run the function, and compare it with an expected result.

The results could be _primitives_ (Int, Bool), or _strings_, or even Abstracts that override `@:op[a == b]` (like Int64). For primitives and strings, checking `solveProblem(input) == expected` works just fine. However, for `Int64` and other more complex objects, it seems to do object-reference equality.

So far, I’ve just been skirting around the issue by returning `toString()` in the implementations where it matters, but it makes me wonder if there’s a cleaner way to achieve the desired comparison.

**How might I use Haxe’s type system to check the equality of arbitrary primitives or objects?** Hopefully in a generic and type-safe manner?

I’ve already been using a `Comparable<T>` typedef elsewhere to enforce the existence of an `equals()` method (for example, on coordinate pair objects), and I wonder if I could somehow use that same type information in this case.

* * *

I did in fact hard-code an Int64 check in my experimentation, which I’ll provide here in case it helps others with brainstorming:

```haxe
using haxe.Int64;

...

if (result.isInt64() && expected.isInt64() && (result : Int64) == (expected : Int64)) {
    Sys.print("✅");
} else if (result == expected) {
    Sys.print("✅");
} else {
    Sys.print("❌");
}

```

---

<div class="post-metadata">

**Author:** ![benmerckx](https://community.haxe.org/user_avatar/community.haxe.org/benmerckx/32/906_2.png) [@benmerckx](https://community.haxe.org/u/benmerckx)\
**Post date:** [December 24, 2024, 8:38pm UTC](https://community.haxe.org/t/equality-check-compatible-with-custom-equals-methods/4493/2 "2024-12-24T20:38:30Z")

</div>

You could check the type of the return value and deal with every case seperately. This can help you on your way: [deep\_equal/src/deepequal/DeepEqual.hx at master · kevinresol/deep\_equal · GitHub](https://github.com/kevinresol/deep_equal/blob/master/src/deepequal/DeepEqual.hx#L76)

---

<div class="post-metadata">

**Author:** ![neillrobson](https://community.haxe.org/user_avatar/community.haxe.org/neillrobson/32/2413_2.png) [@neillrobson](https://community.haxe.org/u/neillrobson)\
**Post date:** [December 31, 2024, 4:12pm UTC](https://community.haxe.org/t/equality-check-compatible-with-custom-equals-methods/4493/3 "2024-12-31T16:12:33Z")

</div>

Yeah, manually checking for each return type is more or less what I’ve resigned myself to. Nothing wrong with that strategy, if that’s an acceptable practice! I just figured that there would be some more idiomatic way to handle checking equality of `Any` or `Dynamic`, with all of Haxe’s reflection & macro capabilities.

Is there specifically a way to check a `Any` instance for conformity with a typedef like `Comparable<T>`? If so, I could make a equality check that _only_ deals with primitives, `Comparable<T>`, and maybe a handful of other special cases like `Int64`.
