在宏中扩展符号变量
Expanding symbol variable inside a macro
class Controller
def index
puts "hello"
end
end
macro handler(controller, action)
c = {{controller}}.new
c.{{action.id}}
end
# this doesn't work
temp = :index
handler Controller, temp
# this works
handler Controller, :index
不知何故,在第一种情况下,宏被扩展为 c.temp
而不是 c.index
是否可以像上面的代码片段那样在 class 中调用函数。
编辑:
我正在努力实现这样的目标,https://github.com/Amber-Crystal/amber/blob/master/src/amber/dsl/router.cr#L16
class Controller
def index
puts "hello"
end
end
macro handler(controller, action)
c = {{controller}}.new
c.{{action.id}}
end
# this doesn't work
temp = :index
handler Controller, temp
# this works
handler Controller, :index
不知何故,在第一种情况下,宏被扩展为 c.temp
而不是 c.index
是否可以像上面的代码片段那样在 class 中调用函数。
编辑: 我正在努力实现这样的目标,https://github.com/Amber-Crystal/amber/blob/master/src/amber/dsl/router.cr#L16