val l = List(1, 2, 3, 4)
l match {
case List(1, 2, 3, 4) => "The list I want"
case List(4, 3, 2, 1) => "This is different"
}
val s = Set(1, 2, 3, 4)
s match {
case Set(4, 3, 2, 1) => "Match !"
case Set(4, _, _, _) => "Match"
case Set(4, 3) => "No match"
}
Why would that matter?
val x = 2
x match {
case 2 =>
case smth =>
}
Cheers,
V
scala> val x=2
x: Int = 2
scala> x match {
| case 2 => "haha"
| case 2 => "hehe"
| case x => x.toString
| }
<console>:11: error: unreachable code
case 2 => "hehe"
^
scala>
scala> val x=2
x: Int = 2
scala> x match {
| case 2 => "haha"
| case 2 => "hehe"
| case x => x.toString
| }
<console>:11: error: unreachable code
case 2 => "hehe"
^
Dear Viktor,object JustInCase {def caseTheJoint( joint : Int ) : Unit = {joint match {case 1234 => println( "yo!" )case 1234 => println( "oy!" )case _ => println( "oyoyo!" )}}}produces the error below from the compiler. This is an example that matches the essential characteristics of this codeobject CaseInPoint {def whatsThePoint( point : Set[Int] ) : Unit = {point match {case Set( 1, 2, 3, 4 ) => println( "yo!" )case Set( 2, 1, 3, 4 ) => println( "oy!" )case _ => println( "oyoyo!" )}}}in that Set( 1, 2, 3, 4 ) and Set( 2, 1, 3, 4 ) not only constants, but the same constant.