我有一個鏈條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
}
錯誤情況下期望的操作是什麼?想要拋出一個異常,打印一些東西並繼續前進,輸出一個錯誤對象? –
打印東西 –