Skip to content

Commit

Permalink
fix GenApplyLensSyntax.value collision
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirkl committed Feb 17, 2019
1 parent 6f3b049 commit ac61eb7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 10 additions & 0 deletions example/src/test/scala/monocle/LensExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ class LensMonoExample extends MonocleSuite {
test("@Lenses is for case classes only") {
illTyped( """@Lenses class C""", "Invalid annotation target: must be a case class")
}

test("GenApplyLensOps has no collision with .value") {
case class MyString(s: String)
object MyString {
implicit class Ops(self: MyString) {
val value: String = self.s
}
}
MyString("a").value shouldEqual "a"
}
}

class LensPolyExample extends MonocleSuite {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@ trait GenApplyLensSyntax {
implicit def toGenApplyLensOps[S](value: S): GenApplyLensOps[S] = new GenApplyLensOps(value)
}

class GenApplyLensOps[A](val value: A) extends AnyVal {
class GenApplyLensOps[A](private val value: A) extends AnyVal {
def lens[C]( field: A => C ): ApplyLens[A,A,C,C] = macro GenApplyLensOpsImpl.lens_impl[A, C]
}

@macrocompat.bundle
class GenApplyLensOpsImpl(val c: blackbox.Context){
def lens_impl[A: c.WeakTypeTag, C](field: c.Expr[A => C]): c.Expr[ApplyLens[A,A,C,C]] = {
import c.universe._

val subj = c.prefix.tree match {
case Apply(TypeApply(_, _), List(x)) => x
case t =>
c.abort(c.enclosingPosition, s"Invalid prefix tree ${show(t)}")
}

c.Expr[ApplyLens[A,A,C,C]](q"""
_root_.monocle.syntax.ApplyLens(
${c.prefix.tree}.value,
$subj,
_root_.monocle.macros.GenLens[${c.weakTypeOf[A]}](${field})
)
""")
Expand Down

0 comments on commit ac61eb7

Please sign in to comment.