Created
July 22, 2012 13:26
-
-
Save masayuki038/3159680 to your computer and use it in GitHub Desktop.
A comparision of Ordered.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.wrap_trap.scala.examples | |
import java.util.Comparator | |
object OrderedCompareTest { | |
def main(args : Array[String]) : Unit = { | |
System.out.println("called main") | |
System.out.println( | |
compareAB(new OrderedClass(2), new OrderedClass(1))) | |
} | |
class OrderedClass(val n: Int) extends Ordered[OrderedClass] { | |
System.out.println("called new OrderedClass") | |
def compare(that: OrderedClass) = this.n - that.n | |
} | |
// View Bound from scala.math.LowPriorityOrderingImplicits | |
implicit def ordered[T <% Comparable[T]]: Ordering[T] = new Ordering[T] { | |
/* = implicit def ordered[T](implicit f: T => Comparable[T]): Ordering[T] = new Ordering[T] { */ | |
System.out.println("called ordered. f: " + implicitly) | |
def compare(x: T, y: T): Int = { | |
x.compareTo(y) | |
} | |
} | |
// Context Bound | |
def compareAB[K:Ordering](a: K, b: K): Int = { | |
/* = def compareAB[K](a: K, b: K)(implicit comp: Ordering[K]): Int = { */ | |
val comp = implicitly | |
System.out.println("called compareAB comp:" + comp) | |
comp.compare(a, b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment