我是 Scala 的新手。最近我在写一个业余爱好应用程序,发现自己在很多情况下试图使用模式匹配代替 if-else。
user.password == enteredPassword match {
case true => println("User is authenticated")
case false => println("Entered password is invalid")
}
而不是
if(user.password == enteredPassword)
println("User is authenticated")
else
println("Entered password is invalid")
这两种方法是否相等? 出于某种原因,其中一种方法是否比另一种方法更可取?