pattern matching itu apaan ya? simplenya sih kayak switch-case di Java, tapi lebih keren lagi kalo di Scala 🙂
Simple Match
gini nih contoh pattern matching di Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val bools = List(true, false, "Eko", "Kurniawan") | |
for (bool <- bools) { | |
bool match { | |
case true => println("BENAR") | |
case false => println("SALAH") | |
case _ => println("GAK TAU APAAN TUH YA") | |
} | |
} |
mirip switch-case nya Java, bedanya pake keyword match-case, kalo di switch ada bagian default jika kondisi case sebelumnya tidak terpenuhi, kalo di match-case gak ada default, gantinya bisa pake _ (garis bawah) yang artinya bisa nampung apapun. Baca lebih lanjut