Anaphory
Anaphory

Reputation: 6432

How to match a Lambda function literal expression

I am trying to transform the code of literal lambda expressions in a Scala-3 macro.

I have defined a macro like this

def rCode(formal: Expr[Int => Boolean])(using Quotes): Expr[Int => Boolean] =
  formal match {
    case '{ (arg: Int) => ${ body }: Boolean } => println(body)
    case f => println(f.show)
  }
  formal

inline def r(inline formal: Int => Boolean): Int => Boolean = ${rCode('formal)}

and in a different file I write

r((arg: Int) => arg == 0)

and the compiler always lands in the second match arm and shows me the entire ((arg: Int => arg == 0).

A comment in How to match function Expr in scala 3 macros mentions that a similar thing didn't work 4 years ago and that the documentation contained a “TODO”. In https://docs.scala-lang.org/scala3/guides/macros/quotes.html#matching-function-expressions I see no TODO, but also no example as general as matching a generic body and argument name.

Have I got a subtle issue or is this still a problem on the Scala side?

Upvotes: 1

Views: 32

Answers (0)

Related Questions