我有一个链条if
/else if
声明不是自我解释。我想用清晰的解释性名称将它们分解为它自己的函数,然后链接这些函数。如何在Scala中重构(if/elsif/elsif)链?
如何在scala中途停止呼叫链?
下面是一个代码示例:
// actual code
for(klass <- program.classes) {
if (complicated boolean) { //checkVars
error1
} else if (complicated boolean) { //checkMethods
error2
} else if (...) { //...
error3
} else {
complicated good case code
}
}
// wanted
for(klass <- program.classes) {
(checkName
andThen checkVars
andThen checkMethods
andThen addToContext) (klass)
// where the chaining stops if a check fails
}
错误情况下期望的操作是什么?想要抛出一个异常,打印一些东西并继续前进,输出一个错误对象? –
打印东西 –