Implicits

原始程式:implicits.scala

/* Defines a new method 'sort' for array objects */
object implicits extends Application {
  implicit def arrayWrapper[A](x: Array[A]) =
    new {
      def sort(p: (A, A) => Boolean) = {
        util.Sorting.stableSort(x, p); x
      }
    }
  val x = Array(2, 3, 1, 4)
  println("x = "+ x.sort((x: Int, y: Int) => x < y))
}

執行結果:

D:\Scala\code>scalac implicits.scala

D:\Scala\code>scala implicits
x = Array(1, 2, 3, 4)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License