[Macros] Passing context instance reference to another class

29 views
Skip to first unread message

Alexander Myltsev

unread,
May 11, 2013, 4:02:04 AM5/11/13
to scala-l...@googlegroups.com
Hi!

I have code:

object Parser {
  type ParserContext = Context { type PrefixType = Parser }

  def ruleImpl(c: ParserContext)(r: c.Expr[Rule]): c.Expr[Boolean] = {
    val opTreeContext = new OpTreeContext(c)
    val opTree = opTreeContext.parse(r.tree) // Compilation error (see below):
Parser.scala: type mismatch;
    opTree.render
  }
}

class OpTreeContext(c: Parser.ParserContext) {
  import c.universe._

  def parse(tree: c.Tree): Expression = ???
}


Compiler is not happy with error:
[error] Parser.scala: type mismatch;
[error]  found   : c.universe.Tree
[error]  required: opTreeContext.c.Tree
[error]     (which expands to)  opTreeContext.c.universe.Tree
[error]     val opTree = opTreeContext.parse(r.tree)
[error]                                        ^



Why type of `c` in OpTreeContext differs from type of `r.tree` in `ruleImpl`? And how to pass instance reference of Parser.ParserContext to another class?

Regards,
  Alexander

Johannes Rudolph

unread,
May 11, 2013, 5:09:30 AM5/11/13
to scala-l...@googlegroups.com
Hi Alexander,

have you seen this section in the documentation:

http://docs.scala-lang.org/overviews/macros/overview.html#writing_bigger_macros

You probably need a type parameter on OpTreeContext for the context.

Johannes
> --
> You received this message because you are subscribed to the Google Groups
> "scala-language" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scala-languag...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Alexander Myltsev

unread,
May 11, 2013, 8:43:52 AM5/11/13
to scala-l...@googlegroups.com, johannes...@googlemail.com
Johaness,

that is exactly I was searching for! I didn't give much attention while was reading at first time. Thanks!

A.
Reply all
Reply to author
Forward
0 new messages