Details

      Description

      import scala.reflect.macros.Context
      import language.experimental.macros
      
      object Macros {
        def getValueImpl[T](c: Context): c.Expr[T] = {
          import c.universe._
          c.Expr[T](Apply(Select(c.prefix.tree, newTermName("getVal")), Nil))
        }
        def setValueImpl[T](c: Context)(value: c.Expr[T]): c.Expr[Unit] = {
          import c.universe._
          c.Expr[Unit](Apply(Select(c.prefix.tree, newTermName("setVal")), List(value.tree)))
        }
      }
      
      object Test extends App {
        object Module {
          def setVal(value: String): Unit = ???
          def getVal(): String = ???
      
          def value: String = macro Macros.getValueImpl[String]
          def value_=(value: String): Unit = macro Macros.setValueImpl[String]
        }
      
        Module.value = ""
      }
      

        Activity

        Hide
        Eugene Burmako added a comment -

        Along the same lines:

        import scala.reflect.macros.Context
        import language.experimental.macros
        
        object Macros {
          def impl(c: Context)(name: c.Expr[String])(value: c.Expr[Any]) = {
            import c.universe._
            reify(println(s"${name.splice} = ${value.splice}"))
          }
        }
        
        import scala.language.dynamics
        
        class C extends Dynamic {
          def updateDynamic(name: String)(value: Any) = macro Macros.impl
        }
        
        object Test extends App {
          val c = new C
          c.foo = 2
        }
        
        16:54 ~/Projects/Kepler_7617/sandbox (ticket/7617)$ ss
        Test.scala:9: error: macros cannot be partially applied
          c.foo = 2
          ^
        one error found
        
        Show
        Eugene Burmako added a comment - Along the same lines: import scala.reflect.macros.Context import language.experimental.macros object Macros { def impl(c: Context)(name: c.Expr[String])(value: c.Expr[Any]) = { import c.universe._ reify(println(s"${name.splice} = ${value.splice}")) } } import scala.language.dynamics class C extends Dynamic { def updateDynamic(name: String)(value: Any) = macro Macros.impl } object Test extends App { val c = new C c.foo = 2 } 16:54 ~/Projects/Kepler_7617/sandbox (ticket/7617)$ ss Test.scala:9: error: macros cannot be partially applied c.foo = 2 ^ one error found
        Show
        Eugene Burmako added a comment - https://github.com/scala/scala/pull/2689

          People

          • Assignee:
            Eugene Burmako
            Reporter:
            Eugene Burmako
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development