Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use q"<foo></foo>" to match the passing tree #1

Open
freewind opened this issue Mar 7, 2015 · 4 comments
Open

Can't use q"<foo></foo>" to match the passing tree #1

freewind opened this issue Mar 7, 2015 · 4 comments

Comments

@freewind
Copy link

freewind commented Mar 7, 2015

For the code:

Macros.manipulate(<foo></foo>)

If I define the manipulate as:

object Macros {
  def manipulate(elem: Elem): Elem = macro Bundle.impl
}

class Bundle(val c: Context) extends MacroLiftables {
  import c.universe._
  def impl(elem: Tree): Tree = {
    val q"<foo></foo>" = elem
    q"$result"
  }
}

It can never match:

[error] /Users/twer/workspace/xml-example/core/src/main/scala/Test.scala:17: exception during macro expansion:
[error] scala.MatchError: {
[error]   {
[error]     new scala.xml.Elem(null, "foo", scala.xml.Null, scala.this.xml.TopScope, false)
[error]   }
[error] } (of class scala.reflect.internal.Trees$Block)
[error]     at Bundle.impl(Macros.scala:17)
[error]   println(Macros.manipulate(<foo></foo>))
[error]                            ^
[error] one error found
[error] (core/compile:compile) Compilation failed
[error] Total time: 2 s, completed Mar 7, 2015 8:40:54 PM
>
@freewind
Copy link
Author

freewind commented Mar 7, 2015

Some investigation:

    def info(s: String) = {
      c.info(c.enclosingPosition, s, force = true)
    }

    info(
      s"""
        |============================
        |passing elem:
        |${showCode(elem)}
        |--------------------------
        |${showCode(q"<foo></foo>")}
        |============================
      """.stripMargin)

Will print:

[info] ============================
[info] passing elem:
[info] {
[info]   {
[info]     new scala.xml.Elem(null, "foo", scala.xml.Null, scala.xml.$scope, false)
[info]   }
[info] }
[info] --------------------------
[info] new _root_.scala.xml.Elem(null, "foo", _root_.scala.xml.Null, scala.xml.TopScope, false)
[info] ============================

@densh
Copy link

densh commented Mar 7, 2015

Hi @freewind, please see this stackoverflow answer.

@freewind
Copy link
Author

freewind commented Mar 7, 2015

Seems I need to write:

val q"${result: scala.xml.Elem}" = elem
val q"<foo></foo>" = q"$result"

It can match this time, is it expected usage?

@freewind
Copy link
Author

freewind commented Mar 7, 2015

Thanks, just noticed you reply, I will see that answer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants