# 基本模式

• 常數模式（Constant pattern）
• 萬用字元模式（Wildcard pattern）
• 建構式模式（Constructor pattern）
• 變數模式（Variable pattern）
• 型別模式（Typed pattern）

``def what(a: Any) = a match {    case 10     => "整數"    case 0.1    => "浮點數"    case 'A'    => "字元"    case true   => "布林值"    case "text" => "字串"    case Nil    => "空串列"     case _      => "？"}``

``case class Point(x: Int, y: Int)def what(a: Any) = a match {    case Point(_, _)   => "圓"    case _             => "不是圓"}println(what(Point(1, 2)))   // 顯示圓println(what(Point(3, 4)))   // 顯示圓println(what("圓？"))         // 顯示不是圓``

``def what(i: Any) = i match {    case 100        => "滿分"    case 90         => "A"    case something  => "不及格？" + something}println(what(100))   // 滿分println(what(90))    // A println(what(80))    // 不及格？80``

``case class Point(x: Int, y: Int)def what(a: Any) = a match {    case Point(x, y)   => "圓 (" + x + ", " + y + ")"    case _             => "不是圓"}println(what(Point(1, 2)))   // 圓 (1, 2)println(what(Point(3, 4)))   // 圓 (3, 4)println(what("圓？"))         // 不是圓``

``val x = 10def what(i: Int) = i match {    case x   => "10"    case _   => "不是 10"}println(what(10))println(what(20))``

error: unreachable code
case _   => "不是 10"
^

``val X = 10def what(i: Int) = i match {    case X   => "10"    case _   => "不是 10"}println(what(10))  // 10println(what(20))  // 不是 10``

``case class Point(x: Int, y: Int)case class Circle(p: Point, r: Int)case class Cylinder(c: Circle, h: Int)def what(a: Any) = a match {    case Point(_, _)                         => "點"    case Circle(Point(_, _), _)              => "圓"    case Cylinder(Circle(Point(_, _), _), _) => "柱"}println(what(Point(10, 10)))                            // 點println(what(Circle(Point(10, 10), 10)))                // 圓println(what(Cylinder(Circle(Point(10, 10), 10), 10)))  // 柱``

``class Point(val x: Int, val y: Int) {    override def equals(a: Any) = a match {        case that: Point => this.x == that.x && this.y == that.y        case _ => false    }    override def hashCode = 41 * (41 + x) + y}``

``class Point(val x: Int, val y: Int) {    override def equals(a: Any) = {        if(a.isInstanceOf[Point]) {            val that = a.asInstanceOf[Point]            this.x == that.x && this.y == that.y        }        false    }        override def hashCode = 41 * (41 + x) + y}``

``def what(a: Any) = a match {    case str : String    => "字串"    case list: List[_]   => "串列"    case set : Set[_]    => "集合"    case map : Map[_, _] => "字典"    case _               => "別的東西"}println(what("text"))         // 字串println(what(List(1, 2)))     // 串列println(what(Set(1, 2, 3)))   // 集合``

``def what(a: Any) = a match {    case list: List[String]  => "字串串列"    case _                   => "別的東西"}``

warning: there were unchecked warnings; re-run with -unchecked for details

warning: non variable type-argument String in type pattern is unchecked since it is eliminated by erasure
case list: List[String]  => "字串串列"
^

``def what(a: Any) = a match {    case list: List[String]  => "字串串列"    case _                   => "別的東西"}val list1 = List("text")val list2 = List(1)println(what(list1))  // 字串串列println(what(list2))  // 字串串列``

``def what(a: Any) = a match {    case arr: Array[Int]     => "整數陣列"    case arr: Array[String]  => "字串陣列"    case _                   => "別的東西"}val arr1 = Array(1)val arr2 = Array("text")println(what(arr1))  // 整數陣列println(what(arr2))  // 字串陣列``