# 上界（Upper bound）、下界（Lower bound）、視界（View bound）

class Animal
class Human extends Animal
class Toy

class Duck[T <: Animal]

val hd = new Duck[Human]
val td = new Duck[Toy] // 編譯錯誤，不符型態邊界

def quick(list: List[Int]): List[Int] = {
list match {
case Nil => Nil
case x::xs =>
val (before,after) = xs partition (_ < x)
quick(before) ++ (x :: quick(after))
}
}

def quick[T <: Ordered[T]](list: List[T]): List[T] = {
list match {
case Nil => Nil
case x::xs =>
val (before,after) = xs partition (_ < x)
quick(before) ++ (x :: quick(after))
}
}

class Ball(val radius: Int) extends Ordered[Ball] {
def volume = 4 * Math.Pi * Math.pow(radius, 3) / 3
override def equals(a: Any) = a match {
case _ => false
}
override def hashCode = 41 * radius
override def toString = "Ball(" + radius + ")"
}

val bs = List(new Ball(3), new Ball(2), new Ball(4), new Ball(1))
quick(bs).foreach(println)

class Alien
class Kryptonian extends Alien

class Earthman
class Spiderman extends Earthman

class Superman[T >: Kryptonian]

val s1 = new Superman[Kryptonian]
val s2 = new Superman[Alien]
val s3 = new Superman[Spiderman] // 編譯錯誤，不符型態邊界

def quick[T <: Ordered[T]](list: List[T]): List[T] = {
list match {
case Nil => Nil
case x::xs =>
val (before,after) = xs partition (_ < x)
quick(before) ++ (x :: quick(after))
}
}

val list = List(3, 1, 2, 4)
quick(list).foreach(println)

def quick[T <% Ordered[T]](list: List[T]): List[T] = {
list match {
case Nil => Nil
case x::xs =>
val (before,after) = xs partition (_ < x)
quick(before) ++ (x :: quick(after))
}
}

val list1 = List(3, 1, 2, 4)
quick(list1).foreach(println)
val list2 = List("xyz", "xab", "abc", "cde")
quick(list2).foreach(println)