2016-12-31 3 views
2

나는 고양이 지금이 Xor이 제거 된 이후 스칼라 고양이 : 확실한 방법이 있습니까?

Xor.right(data).ensure(List(s"$name cannot be blank"))(_.nonEmpty) 

Xor 객체에이 코드 조각이, 내가 사용하는 비슷한 쓰기 어느

Either.ensuring(name.nonEmpty, List(s"$name cannot be blank")) 

객체하려고하지만이 작동하지 않습니다 반환 유형이 보장이기 때문에 Either.type

나는 쓸 수 있습니다. 하지만 고양이 구조로 검증을하고 싶습니다.

답변

3

Xor은 Scala 2.12에서 Either이 오른쪽 바이어스 되었기 때문에 고양이에서 제거되었습니다. 고양이를 사용

val e: Either[String, List[Int]] = Right(List(1, 2, 3)) 
val e2: Either[String, List[Int]] = Right(List.empty[Int]) 

scala> e.filterOrElse(_.nonEmpty, "Must not be empty") 
res2: scala.util.Either[String,List[Int]] = Right(List(1, 2, 3)) 

scala> e2.filterOrElse(_.nonEmpty, "Must not be empty") 
res3: scala.util.Either[String,List[Int]] = Left(Must not be empty) 

, 당신은 Eitherensure를 사용할 수있는 매개 변수 및 무두질 부족의 순서는 없습니다하는 경우 : 당신은 같은 일을하지만, 카레되지 않은 표준 라이브러리 Either#filterOrElse을 사용할 수 있습니다 취향 :

import cats.syntax.either._ 

scala> e.ensure("Must be non-empty")(_.nonEmpty) 
res0: Either[String,List[Int]] = Right(List(1, 2, 3))