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("❌");
}