fork download
  1. sealed abstract class SmartTime extends Ordered[SmartTime] { x =>
  2. def compare(y: SmartTime) = {
  3. x match {
  4. case InfiniteFuture => 1
  5. case InfinitePast => -1
  6. case ConcreteTime(x) =>
  7. y match {
  8. case InfiniteFuture => -1
  9. case InfinitePast => 1
  10. case ConcreteTime(y) => x compare y
  11. }
  12. }
  13. }
  14. }
  15. case class ConcreteTime(t: Long) extends SmartTime
  16. case object InfiniteFuture extends SmartTime
  17. case object InfinitePast extends SmartTime
  18.  
  19. object Main {
  20. def main(args: Array[String]): Unit = {
  21. val y = ConcreteTime(100)
  22. val z = ConcreteTime(10)
  23. val x = InfiniteFuture
  24. val p = InfinitePast
  25. println(Vector(y, z, x, p).sortWith(_ < _))
  26. }
  27. }
Success #stdin #stdout 0.3s 247424KB
stdin
Standard input is empty
stdout
Vector(InfinitePast, ConcreteTime(10), ConcreteTime(100), InfiniteFuture)