From ac61eb789557adcb9a59fca732c161758e7100d8 Mon Sep 17 00:00:00 2001 From: Vladimir Klyushnikov <72238+vladimirkl@users.noreply.github.com> Date: Sun, 17 Feb 2019 17:32:01 +0300 Subject: [PATCH] fix GenApplyLensSyntax.value collision --- example/src/test/scala/monocle/LensExample.scala | 10 ++++++++++ .../monocle/macros/syntax/GenApplyLensSyntax.scala | 11 +++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/example/src/test/scala/monocle/LensExample.scala b/example/src/test/scala/monocle/LensExample.scala index 6cab5ef9d..390ec1d4e 100644 --- a/example/src/test/scala/monocle/LensExample.scala +++ b/example/src/test/scala/monocle/LensExample.scala @@ -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 { diff --git a/macro/shared/src/main/scala/monocle/macros/syntax/GenApplyLensSyntax.scala b/macro/shared/src/main/scala/monocle/macros/syntax/GenApplyLensSyntax.scala index 59a0fc8af..215ce6afc 100644 --- a/macro/shared/src/main/scala/monocle/macros/syntax/GenApplyLensSyntax.scala +++ b/macro/shared/src/main/scala/monocle/macros/syntax/GenApplyLensSyntax.scala @@ -6,7 +6,7 @@ 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] } @@ -14,9 +14,16 @@ class GenApplyLensOps[A](val value: A) extends AnyVal { 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}) ) """)