Last active
October 16, 2017 12:51
-
-
Save loicknuchel/a08a7efedc0e9877f1b53f4ba0112b20 to your computer and use it in GitHub Desktop.
How implicit class resolution works ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalatest.{FunSpec, Matchers} | |
object Implicits { | |
implicit class SeqEitherExtension[E, A](val elt: Seq[Either[E, A]]) extends AnyVal { | |
def partition: (Seq[E], Seq[A]) = { | |
val (left, right) = elt.partition(_.isLeft) | |
(left.collect { case Left(e) => e }, right.collect { case Right(a) => a }) | |
} | |
} | |
} | |
class ImplicitsSpec extends FunSpec with Matchers { | |
describe("SeqEitherExtension") { | |
import Implicits._ | |
it("partition") { | |
// works | |
SeqEitherExtension(Seq(Left("err"), Right(1))).partition shouldBe(Seq("err"), Seq(1)) | |
// do not work, implicit not found :( | |
// Any idea on why implicit class is not found ??? | |
Seq(Left("err"), Right(1)).partition shouldBe(Seq("err"), Seq(1)) | |
/** | |
* Error:(24, 34) missing argument list for method partition in trait TraversableLike | |
* Unapplied methods are only converted to functions when a function type is expected. | |
* You can make this conversion explicit by writing `partition _` or `partition(_)` instead of `partition`. | |
* Seq(Left("err"), Right(1)).partition shouldBe(Seq("err"), Seq(1)) | |
*/ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from your comment it sounds like the parameterless case is processed under