" method? Is it possible to define it in general terms for all types that have ...">

How to determine the maximum function for each type using the ">" method?

Is it possible to define it in general terms for all types that have a method >(also all AnyVals), so it is enough to use one implementation method?

+3
source share
3 answers

You can declare an implicit conversion:

implicit def greater2order[A](self : { def >(that : A) }) : Order[A] = ...

and then just use scalaz ...

+4
source

This standard library already does this. But without assigning a type to an object having a method >...

List(1,2,3).max(Ordering.fromLessThan( (a:Int, b:Int) => b > a) )

, , > - >. , max.

:

case class S(s:String) {
  def >(that:S) = java.text.Collator.getInstance.compare(s, that.s) > 0
}

List(S("abc"), S("ABa"), S("abd")).max(Ordering.fromLessThan( (a:S,b:S) => b>a) )
// res9: S = S(abd)
+3

The order value may be relevant.

It covers standard numeric types (and then some — see “Known Subclasses”). Implicits can be used to “open” by external types.

Happy coding.

+1
source

All Articles