我想在Scala中进行模式匹配,但需要忽略大小写。有没有一种方法可以在不使用单独的小写和大写'case'子句的情况下编写代码。
//person class with first name and last name
case class Person (var fn: String, val ln: String) {
val name = fn
val lastName = ln
}
//two instances. Same last name but cases are different
val a2 = Person("Andy","Cale")
val a3 = Person("Andy","cale")
def isCale(person:Person) {
person match {
//I want that this case should be case insensitive
case Person(_,"Cale") => println("last-name Cale")
case _ => println("not Cale")
}
}
isCale(a2)
lastname Cale
//I want this to also match
isCale(a3)
not Cale
一种替代方案是提取姓氏并进行比较,但我想知道是否有一种在案例本身中完成这个任务的方法。
def isCale(a2:A2) {
val s = a2.ln
s.toLowerCase match {
case "cale" => println("last-name Cale")
case _ => println("not Cale")
}