Option
에 Seq
과 같은 개체를 래핑하는 다음과 같은 메서드가 있습니다.부분적으로 적용된 메서드에 대해 암시 적 뷰를 사용할 수 없음
def noneIfEmpty[S <% Seq[_]](seq: S): Option[S] = {
if (seq.isEmpty) None else Some(seq)
}
나는 Try
에 싸여 계산의 결과를 변환하기 위해이 방법을 사용할 수 있도록하고 싶습니다. 나는 List[Int]
이 할 말 :
scala> val tryList = Try(List(1,2,3))
tryList: scala.util.Try[List[Int]] = Success(List(1, 2, 3))
나는 Try[Option[List[Int]]]
에 Try[List[Int]]
를 매핑 noneIfEmpty
을 사용할 수 있어야합니다. 나는 익명 함수를 사용하여 명시 적으로 noneIfEmpty
에 목록을 전달하면이 ...
scala> tryList map (list => noneIfEmpty(list))
res1: scala.util.Try[Option[List[Int]]] = Success(Some(List(1, 2, 3)))
을 잘 작동 ...하지만 부분적으로 적용 함수로 noneIfEmpty
을 통과하려고하면이 나옵니다.
scala> tryList map noneIfEmpty _
<console>:40: error: No implicit view available from S => Seq[_].
tryList map noneIfEmpty _
^
내가 noneIfEmpty
만 목록을 받아 좁힐 경우도 잘 작동 :
scala> def noneIfEmptyList[A](list: List[A]): Option[List[A]] = noneIfEmpty(list)
noneIfEmptyList: [A](list: List[A])Option[List[A]]
scala> tryList map noneIfEmptyList _
res2: scala.util.Try[Option[List[Int]]] = Success(Some(List(1, 2, 3)))
여기 무슨 일이야? 직장이나 다른 곳에서 일종의 소거 부두가 있습니까?