我是scala的新手。我想知道是否有更好的方法可以更好地编写以下if-else语句。这纯粹是我的学习
val tokenizationRequired = if (args(4).equals("true")) true else false
if (tokenizationRequired) {
primary_key = args(5)
if (primary_key.equals("") || primary_key.isEmpty) {
log info s"Primary_Key cannot be empty"
}
lookupPath = args(6)
if (lookupPath.equals("") || lookupPath.isEmpty) {
log info s"lookupPath cannot be empty"
}
}
这是我尝试过的无效案例
val primary_key = args(5) match {
case " " => log info s"lookupPath cannot be empty"
case _ => args(5)
}
感谢您的帮助。
答案 0 :(得分:7)
val tokenizationRequired = args(4).toBoolean
val primary_key = args(5)
val lookupPath = args(6)
if (tokenizationRequired && primary_key.isEmpty) {
log info s"Primary_Key cannot be empty"
}
if (tokenizationRequired && lookupPath.isEmpty) {
log info s"lookupPath cannot be empty"
}
这是我尝试过的无效的案例陈述...
这仅在scala 2.13中有效:
import scala.util.chaining._
val primary_key = arg(5)
.tap(x => if (x.isEmpty) log info s"lookupPath cannot be empty")
,您的版本将使用此修复程序进行编译:
val primary_key = args(5) match {
case x if x.isEmpty =>
log info s"lookupPath cannot be empty"
x
case x => x
}