Skip to content

Improve public AST creation facilities #5917

Closed

Description

@scabug

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

Activity

  1. scabug commented on Jun 15, 2012

    @scabug
    Author

    Imported From: https://issues.scala-lang.org/browse/SI-5917?orig=1
    Reporter: @xeno-by
    Affected Versions: 2.10.0

  2. scabug commented on Jun 28, 2012

    @scabug
    Author

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

  3. scabug commented on Jul 12, 2012

    @scabug
    Author

    @xeno-by said (edited on Mar 3, 2014 10:58:41 AM UTC):
    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.

  4. scabug commented on Jul 11, 2013

    @scabug
    Author

    @xeno-by said:
    Fixed in scala/scala#2714

  5. added this to the 2.11.0-M4 milestone on Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions