Scala Programming Language
  1. Scala Programming Language
  2. SI-5774

Macros should be able to inject (implicit) definitions into their calling context

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Out of Scope
    • Affects Version/s: Scala 2.10.0
    • Fix Version/s: None
    • Component/s: Macros
    • Labels:
      None

      Description

      It would be very useful if we could we write a macro,

      def publish[T](t : T) = macro publishImpl(t)
      
      def publishImpl[T](c : Context)(t : c.Expr[T]) = ...
      

      such that when called,

      publish("foo")
      

      it expanded at the call site to,

      implicit val <freshname> = "foo"
      

      The following implementation suggested by Eugene,

      import scala.reflect.makro.Context
      
      object Macros {
         def publish = macro publishImpl
      
         def publishImpl(c : Context) = c.reify{implicit val foo: String = "foo"}
      }
      
      import Macros._
      
      object Test extends App {
        publish;
        println(implicitly[String])
      }
      

      doesn't work "because (as far as I can guess by interpreting the error) macro expansion happens too late to be included in scope".

        Activity

        Show
        Eugene Burmako added a comment - Very enlightening discussion: http://groups.google.com/group/scala-internals/browse_thread/thread/cef63f151de17494
        Hide
        Eugene Burmako added a comment -

        Looks like now that we have working implicit macros, this is no longer necessary.

        Show
        Eugene Burmako added a comment - Looks like now that we have working implicit macros, this is no longer necessary.

          People

          • Assignee:
            Eugene Burmako
            Reporter:
            Miles Sabin
          • Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development