1

I would like to pattern match for the tree of the x -> y operation in Scala macros. I am cross compiling against Scala 2.10.4 (with Macro Paradise) and Scala 2.11.x. I have tried the following patterns and none worked:

arrowTree match {
  case q"$x -> $y"                                            => ???
  case q"scala.Predef.ArrowAssoc[${_}, ${_}]($x).->$y"        => ???
  case q"_root_.scala.Predef.ArrowAssoc[${_}, ${_}]($x).->$y" => ???
  case q"_root_.scala.Predef.ArrowAssoc($x).->$y"             => ???
}

What patterns would make this match work for both 2.10.4 and 2.11.x?

vjovanov
  • 120
  • 5

1 Answers1

2

In a perfect world you'd definitely expect first one to match but it's not that simple.

Whenever you write foo -> bar in Scala you effectively calling a method like foo.->(bar). When typechecker tries to find a method called -> it fails and starts looking for any implicits that could have added that method to foo and it usually finds a Predef.ArrowAssoc which turns original expression into scala.this.Predef.ArrowAssoc[Int](1).->[Int](2).

To match this you can use following pattern:

case q"scala.this.Predef.ArrowAssoc[$_]($foo).->[$_]($bar)" =>

Here we put wildcards in a type parameter position as we don't care about those at the moment.

On 2.10 the pattern would be slightly different because implicit was named differently back then:

case q"scala.this.Predef.any2ArrowAssoc[${_}]($foo).->[${_}]($bar)" =>

Another difference here is need to use curly braces around wildcards. $_ syntax in string interpolators isn't supported on 2.10.

Denys Shabalin
  • 1,696
  • 12
  • 12