Equality check compatible with custom `equals()` methods

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

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:

using haxe.Int64;

...

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

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

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.