def matchAny(any: Any) = any match { case "Huhu" => println("Hi") case 5 => println("high five"); case a: Int => println("Ich kann bis " + a + " zaehlen") case a: String => println("Ein String") case (a,b) => println("Tuple mit Werten " + a + "und" + b ) case Nil => println("Eine leere Liste") case List(1,2,3) => println("Liste mit den Werten 1,2,3") case List(a,_*) => println("Liste Laenge > 2 und den Werten + " + a + ",*,...") case _ => println("Niemand passt zu: " + any) } println(matchAny("Huhu")) println(matchAny("5")) println(matchAny(10)) class Person(name: String, weight: Int) { } val horst = (new Person("Horst", 55)) //compiler ignoriert fachliche Fehler println(matchAny(horst)) println(List(horst, new Person("Anna", 105))) val desires = (horst, "Spank me!") println(matchAny(desires)) println(matchAny(List(1,2))) println(matchAny(List(1,2,3))) println(matchAny(List(1,2,3,4,5)))
Friday, September 28, 2012
Pattern Matching Basics
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment