小编Jaw*_*oah的帖子

没有隐式视图可用于部分应用的方法

所以我有以下方法包装一个类似Seq的对象Option.

def noneIfEmpty[S <% Seq[_]](seq: S): Option[S] = {
  if (seq.isEmpty) None else Some(seq)
}
Run Code Online (Sandbox Code Playgroud)

我希望能够使用这种方法来转换包含在中的计算结果Try.说我这样做List[Int]:

scala> val tryList = Try(List(1,2,3))
tryList: scala.util.Try[List[Int]] = Success(List(1, 2, 3))
Run Code Online (Sandbox Code Playgroud)

我应该可以noneIfEmpty用来映射Try[List[Int]]到a Try[Option[List[Int]]].如果我使用匿名函数并明确地将列表传递给noneIfEmpty...,这可以正常工作

scala> tryList map (list => noneIfEmpty(list))
res1: scala.util.Try[Option[List[Int]]] = Success(Some(List(1, 2, 3)))
Run Code Online (Sandbox Code Playgroud)

...但是如果我尝试noneIfEmpty作为部分应用函数传递它会中断.

scala> tryList map noneIfEmpty _
<console>:40: error: No implicit view available from S => Seq[_].
              tryList map noneIfEmpty _
                          ^
Run Code Online (Sandbox Code Playgroud)

如果我缩小 …

scala implicit implicit-conversion

3
推荐指数
1
解决办法
3202
查看次数

标签 统计

implicit ×1

implicit-conversion ×1

scala ×1