blue-sky blue-sky - 3 months ago 12
Scala Question

What is the use of this case of scalaz.Ordering?

Reading http://eed3si9n.com/learning-scalaz/Order.html this example is given :

scala> 1.0 ?|? 2.0
res10: scalaz.Ordering = LT


All that is stated with regard to Ordering is :

"

scala> 1 > 2.0
res8: Boolean = false

scala> 1 gt 2.0
<console>:14: error: could not find implicit value for parameter F0: scalaz.Order[Any]
1 gt 2.0
^

scala> 1.0 ?|? 2.0
res10: scalaz.Ordering = LT

scala> 1.0 max 2.0
res11: Double = 2.0

Order enables ?|? syntax which returns Ordering: LT, GT, and EQ. It also enables lt, gt, lte, gte, min, and max operators by declaring order method. Similar to Equal, comparing Int and Doubl fails compilation.


"

What is the significance of scalaz.Ordering in this case ?

1.0 max 2.0 is using standard Scala API max method and does not relate to scalaz ?

Answer

Scalaz does provide this syntax for things with an Order instance. You're right in this case that 1.0 max 2.0 will use the max from the standard library, though, since it's more specific (as you can quickly confirm by starting up power mode with :power in the REPL and then entering something like settings.processArgumentString("-print typer")).

A better example in this case would be something like some(2.0) max some(1.0), since the standard library doesn't provide a max for Option[Double] that would hide Scalaz's.

Comments