Nezteb
07/29/2020, 4:55 PMcompareOp(v1: Any, v2: Any, f: (Any, Any) -> Boolean): Boolean
...
compareOp(v1, v2, {x, y -> x < y})
This isn’t quite right though. The “<” on the last line doesn’t compile. Any < Any doesn’t exist.
Since I’m only trying to compare as strings or numbers, it’s possible to write this as:
compareOp(v1: Any, v2: Any, fs: (String, String) -> Boolean, fd: (Double, Double) -> Boolean): Boolean
...
compareOp(v1, v2, {x, y -> x < y}, {x, y -> x < y})
That works, but still doesn’t feel right. {x, y -> x < y} is duplicated. I should be able to pass it in just once.
Its signature is:
<T> fun comparison(x: Comparable<T>, y: T): Boolean
As far as I can tell, it’s not possible to stick this type parameter in the signature of compareOp.
I tried making an interface Comparison<T> with an “operator fun invoke” that has the Comparable<T> signature. That still doesn’t quite work. compareOp has to specify a single explicit value for T.
Has anyone solved this sort of issue?Shawn
07/29/2020, 5:08 PMAny
?Shawn
07/29/2020, 5:09 PMfun <T : Comparable<T>> compareOp(first: T, second: T, comparator: (T, T) -> Boolean)