Scala 将函数列表传递给稍后使用提供的数据执行的方法
我正在尝试构建一个相当通用的多级Scala 将函数列表传递给稍后使用提供的数据执行的方法,scala,validation,recursion,Scala,Validation,Recursion,我正在尝试构建一个相当通用的多级验证器对象。我们的想法是,如果级别1通过了,那么就要进行级别2的验证,等等。但我正在努力解决一个特定的问题:创建一个函数调用,但直到稍后才执行它 数据: 验证错误: case class ValidationError(code: String, message: String) 验证器: object Validator { def validate(validations: List[List[Validation]]): List[Validat
验证器对象。我们的想法是,如果级别1通过了,那么就要进行级别2的验证,等等。但我正在努力解决一个特定的问题:创建一个函数调用,但直到稍后才执行它
数据:
验证错误:
case class ValidationError(code: String, message: String)
验证器:
object Validator {
def validate(validations: List[List[Validation]]): List[ValidationError] = {
validations match {
case head :: nil => // Execute the functions and get the results back
// Recursively work down the levels (below syntax may be incorrect)
case head :: tail => validate(head) ... // if no errors then validate(tail) etc.
...
}
}
}
object CorrectNameFormatValidator extends Validation {
def validate(str: String): Seq[ValidationError] = {
...
}
}
样本验证器:
object Validator {
def validate(validations: List[List[Validation]]): List[ValidationError] = {
validations match {
case head :: nil => // Execute the functions and get the results back
// Recursively work down the levels (below syntax may be incorrect)
case head :: tail => validate(head) ... // if no errors then validate(tail) etc.
...
}
}
}
object CorrectNameFormatValidator extends Validation {
def validate(str: String): Seq[ValidationError] = {
...
}
}
我希望如何使用它:
object App {
def main(args: Array[String]): Unit = {
val fooData = FooData(alpha = "first", beta = "second")
val levelOneValidations = List(
CorrectNameFormatValidator(fooData.alpha),
CorrectNameFormatValidator(fooData.beta),
SomeOtherValidator(fooData.beta)
)
// I don't want these to execute as function calls here
val levelTwoValidations = List(
SomeLevelTwoValidator (fooData.alpha),
SomeLevelTwoValidator(fooData.beta),
SomeOtherLevelValidator(fooData.beta),
SomeOtherLevelValidator(fooData.alpha)
)
val validationLevels = List(levelOneValidations, levelTwoValidations)
Validator.validate(validationLevels)
}
}
当我不需要的时候,我是在做一些非常复杂的事情,还是我只是缺少了一个组件
本质上,我想定义何时调用函数以及使用哪些参数,但我不希望调用发生,直到我说在验证器中。这是可能的吗?在定义levelOneValidation
、levelTwoValidations
和validationLevel
时,可以使用lazy val
或def:
object App {
def main(args: Array[String]): Unit = {
val fooData = FooData(alpha = "first", beta = "second")
lazy val levelOneValidations = List(
CorrectNameFormatValidator(fooData.alpha),
CorrectNameFormatValidator(fooData.beta),
SomeOtherValidator(fooData.beta)
)
// I don't want these to execute as function calls here
lazy val levelTwoValidations = List(
SomeLevelTwoValidator (fooData.alpha),
SomeLevelTwoValidator(fooData.beta),
SomeOtherLevelValidator(fooData.beta),
SomeOtherLevelValidator(fooData.alpha)
)
lazy val validationLevels = List(levelOneValidations, levelTwoValidations)
Validator.validate(validationLevels)
}
}
您还需要更改validate
方法以获得验证的名称,而不是
ByValue
使用:=>
:
object Validator {
def validate(validations: => List[List[Validation]]): List[ValidationError] = {
validations match {
case head :: nil => // Execute the functions and get the results back
// Recursively work down the levels (below syntax may be incorrect)
case head :: tail => validate(head) ... // if no errors then validate(tail) etc.
...
}
}
}
无论如何,我认为您可以通过使用一些OOP设计模式来实现它,比如责任链 你能再解释一下吗?我猜文本很复杂,请列出leveltwovalizations
adef
而不是val
。然后,验证将在引用时而不是定义时执行。关键部分是lazy val
和使用def
而不是val
,这就成功了。我研究了责任链模式,并对代码进行了重构。感谢您@user3725190和@jwvh