Add Equal 'type class'
I think fugue should provide a Equal type class similar to https://github.com/functionaljava/functionaljava/blob/master/core/src/main/java/fj/Equal.java
- It would provide additional type-safety over
Object.equals
- It would allow to test for equality of objects from classes that do not implement
equals
or implement it not the way you want. (eg. it would permit to compareIterable
or compareBigDecimal
viacompareTo()
instead ofequals()
...)
Equal
is also a quasi-requirement for sane property based testing (monoids and optics).
Maybe also Hash and Show type class sould be considered for addition. But Equal is the most useful.
Comments (5)
-
reporter -
Does this type class specify the level of equality that makes sense or is it up to the implementations to decide on things like value vs referential equality?
Can you expand on the type of code you'd like to write that you can write with a comparator (or helper methods like mkPred(Comparator<A>c , A a): Predicate<A>)?
-
reporter Equal is for mathematical equality, in that sense: 1.00 == 1.0 Also useful for types that cannot implement
Object.equals
in any meaningful way. eg for types like Iterable<BigDecimal>, I would need something like:static <A> Equal<Iterable<A>> iterableEqual(Equal<A> aEqual)
-
reporter Equal may also be useful for floating point arithmetic. Since double addition is not associative in general -
(0.1 + 0.2) + 0.3 == 0.1 + (0.2 + 0.3)
returnfalse
- to want to use algorithm that abstract over Monoids with Double then you may need to define equality within an approximation that is 'morally' acceptable for your program. Hence a customized Equal<Double>.But this may be a minor use case for an
Equal
typeclass.The additional type-safety and possibility to define equality for eg.
Iterable
is what make it really useful. FJ, scalaz, haskell concur on this. -
- changed status to resolved
timing out
- Log in to comment
Something like: