Scala 3中的fusemap宏
遵循元编程教程在这里fusemap示例可以使用。我拥有的宏是(在宏中
对象)
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) }
,并像我期望看到在Compile上打印出的简化代码一样在单独的文件中调用
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")
}
,但是我看到“ Fusemap无需任何事情”。
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过将论点提交
简化
内联来获得这项工作I was able to get this work by making the argument to
simplify
inline like