Details

    • Type: Improvement
    • Status: CLOSED
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: Scala 2.10.0
    • Fix Version/s: Scala 2.11.0-M4
    • Component/s: Macros, Reflection
    • Labels:
      None

      Description

      Currently there does exist a simple way to create trees (reify), but its applicability is limited to static templates with holes having statically known types. However when writing macros, there's frequently a need in something more sophisticated.

      This is an open research question, and there are several possible approaches to solving this problem:
      1) Classic quasiquotes that use string interpolation syntax: https://github.com/scalamacros/kepler/tree/topic/interpolation
      2) TreeHugger-like DSL: http://eed3si9n.com/treehugger/
      3) Improved reify that would work with partially typed trees

        Attachments

          Activity

          Hide
          burmako Eugene Burmako added a comment -

          There's also an interesting approach to pattern matching against trees: https://github.com/ochafik/Scalaxy/wiki/Scalaxy-Compilets.

          Show
          burmako Eugene Burmako added a comment - There's also an interesting approach to pattern matching against trees: https://github.com/ochafik/Scalaxy/wiki/Scalaxy-Compilets .
          Hide
          burmako Eugene Burmako added a comment - - edited

          Nice example which illustrates once again why reify isn't always applicable:

           def pack[A](a: A): Array[Byte] = macro pack_impl[A]
           
             def pack_impl[A: c.TypeTag](c: Context)(a: c.Expr[A]): c.Expr[Array[Byte]] = {
               import c.universe._
               c.reify {
                 val baos = new ByteArrayOutputStream
                 packto_impl(c)(c.reify[OutputStream]{ baos }, a).splice
                 // works: packto_impl(c)(c.Expr[OutputStream](Ident("baos")), a).splice
                 // doesn't work: packto_impl(c)(c.reify[OutputStream]{ baos }, a).splice
                 baos.toByteArray
               }
             }
           
             def packto[A](output: OutputStream, value: A): Unit = macro packto_impl[A]
           
             def packto_impl[A: c.TypeTag](c: Context)(output: c.Expr[OutputStream], value: c.Expr[A]): c.Expr[Unit] = { .... }
          

          Compiling this code will signalize about cross-stage evaluation. This is justified, because the value of baos (as a free variable) will be captured by reification. But this value is only available at runtime, while splice in question needs to work at compile-time.

          This is all reasonable and stuff from the staging point of view, but it doesn't really matter for the programmer who just wants to quasiquote.

          Show
          burmako Eugene Burmako added a comment - - edited Nice example which illustrates once again why reify isn't always applicable: def pack[A](a: A): Array[Byte] = macro pack_impl[A]   def pack_impl[A: c.TypeTag](c: Context)(a: c.Expr[A]): c.Expr[Array[Byte]] = { import c.universe._ c.reify { val baos = new ByteArrayOutputStream packto_impl(c)(c.reify[OutputStream]{ baos }, a).splice // works: packto_impl(c)(c.Expr[OutputStream](Ident("baos")), a).splice // doesn't work: packto_impl(c)(c.reify[OutputStream]{ baos }, a).splice baos.toByteArray } }   def packto[A](output: OutputStream, value: A): Unit = macro packto_impl[A]   def packto_impl[A: c.TypeTag](c: Context)(output: c.Expr[OutputStream], value: c.Expr[A]): c.Expr[Unit] = { .... } Compiling this code will signalize about cross-stage evaluation. This is justified, because the value of baos (as a free variable) will be captured by reification. But this value is only available at runtime, while splice in question needs to work at compile-time. This is all reasonable and stuff from the staging point of view, but it doesn't really matter for the programmer who just wants to quasiquote.
          Hide
          burmako Eugene Burmako added a comment -
          Show
          burmako Eugene Burmako added a comment - Fixed in https://github.com/scala/scala/pull/2714

            People

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

              Dates

              • Created:
                Updated:
                Resolved: