Following the metaprogramming tutorial here, I am unable to get the fuseMap example to work. The macro I have is (in macros
object)
def fuseMap[T: Type](x: Expr[List[T]])(using Quotes): Expr[List[T]] =
println("Initial passed in x: " + x.show)
x match {
case '{
type u
type v
($ls: List[`u`])
.map($f: `u` => `v`)
.map($g: `v` => T)
} =>
'{ $ls.map(y => $g($f(y))) }
val result = '{ $ls.map(y => $g($f(y))) }
println(result.show)
result
case _ =>
println("fuseMap didn't do anything")
x
}
inline def simplify[T](x: List[T]): List[T] = ${ fuseMap('x) }
And calling it in seperate file like
object macrofun extends App {
val l = List(1, 2, 3)
val f = (x: Int) => x + 1
val g = (y: Int) => y * 2
val r = simplify {
List(1, 2, 3)
.map(f)
.map(g)
}
println(s"Result of l is $r")
}
I would expect to see the simplified code printed out on compile but instead I see "fuseMap didn't do anything".