Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 使用EitherT-s inside对comp进行数据验证_Scala_Validation_Design Patterns_Functional Programming_For Comprehension - Fatal编程技术网

Scala 使用EitherT-s inside对comp进行数据验证

Scala 使用EitherT-s inside对comp进行数据验证,scala,validation,design-patterns,functional-programming,for-comprehension,Scala,Validation,Design Patterns,Functional Programming,For Comprehension,考虑一下我试图解决的问题的简化版本。这是我当前的实现 case class Foo() def addFoo(json: String): EitherT[Future, Exception, Long] = { def parse(json: String): EitherT[Future, Exception, Foo] = ??? def validate(foo: Foo): EitherT[Future, Exception, Foo] = ??? def save(

考虑一下我试图解决的问题的简化版本。这是我当前的实现

case class Foo()

def addFoo(json: String): EitherT[Future, Exception, Long] = {
  def parse(json: String): EitherT[Future, Exception, Foo] = ???

  def validate(foo: Foo): EitherT[Future, Exception, Foo] = ???

  def save(foo: Foo): EitherT[Future, Exception, Long] = ???

  for {
    foo <- parse(json)
    _ <- validate(foo)
    id <- save(foo)
  } yield id
}
case类Foo()
def addFoo(json:String):EitherT[Future,Exception,Long]={
def parse(json:String):EitherT[Future,Exception,Foo]=???
def验证(foo:foo):EitherT[Future,Exception,foo]=???
def save(foo:foo):EitherT[未来、异常、长]=???
为了{

foo如果您不关心正确的值,您可以使用
Unit
而不是
foo
,例如:

def validate(foo: Foo): EitherT[Future, Exception, Unit] = ???

您可以更改
validate
的类型签名以添加更多类型安全性:

def-validate(foo:foo):EitherT[未来、异常、有效foo]

其中
ValidFoo
如下所示:

object ValidFoo {
  def believeMeItsValid(raw: Foo): ValidFoo = new ValidFoo(raw)
}

class ValidFoo private (val foo: Foo)
save
将只接受已验证的输入:

def save(foo:ValidFoo):EitherT[未来、异常、长]

这将迫使您在保存输入之前验证输入,并有助于防止布尔盲