There is no implicit representation for a partially applied method

So, I have the following method that wraps a Seq

-like object in Option

.

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

      

I would like to use this method to transform the results of a computation wrapped in Try

. Let's say I do it with List[Int]

:

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

      

I have to use noneIfEmpty

to match Try[List[Int]]

with Try[Option[List[Int]]]

. This works great if I use an anonymous function and pass the list explicitly noneIfEmpty

...

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

      

... but it breaks if I try to pass noneIfEmpty

as a partially applied function.

scala> tryList map noneIfEmpty _
<console>:40: error: No implicit view available from S => Seq[_].
              tryList map noneIfEmpty _
                          ^

      

It also works great if I judge noneIfEmpty

to only accept lists:

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)))

      

What's going on here? Is there some type of voodoo erasure at work or something else?

+3


source to share


1 answer


The problem is that the view constraint is syntactic sugar for an implicit parameter, so your method is indeed

def noneIfEmpty[S](seq: S)(implicit ev: S => Seq[_]): Option[S]

      



Unfortunately, you cannot partially apply a method that takes implicit parameters, since the eta extension process (i.e., converting a method to a function) requires implications to be resolved beforehand.

Ultimately your problem comes down to this .

+5


source







All Articles