相关疑难解决方法(0)

Scala中的方法参数验证,用于理解和monad

我正在尝试验证无效方法的参数,但我找不到解决方案......

谁能告诉我该怎么办?

我正在尝试这样的事情:

  def buildNormalCategory(user: User, parent: Category, name: String, description: String): Either[Error,Category] = {
    val errors: Option[String] = for {
      _ <- Option(user).toRight("User is mandatory for a normal category").right
      _ <- Option(parent).toRight("Parent category is mandatory for a normal category").right
      _ <- Option(name).toRight("Name is mandatory for a normal category").right
      errors : Option[String] <- Option(description).toRight("Description is mandatory for a normal category").left.toOption
    } yield errors
    errors match {
      case Some(errorString) => Left( Error(Error.FORBIDDEN,errorString) )
      case None =>  Right( buildTrashCategory(user) )
    }
  }
Run Code Online (Sandbox Code Playgroud)

monads scala either for-comprehension

33
推荐指数
3
解决办法
7950
查看次数

标签 统计

either ×1

for-comprehension ×1

monads ×1

scala ×1