【发布时间】:2016-09-03 14:43:30
【问题描述】:
当used 时,我正在开发一个扩展其他模块功能的模块。问题是有时我想在模块定义中编写一些用于增强模块功能的自定义代码:
所以,它看起来像:
defmodule Included do
defmacro __using__(_) do
quote unquote: false do
# a lot of code
model
|> unquote(@callback)
|> another_bunch_of_things_in_pipe
end
end
end
还有我的模块
defmodule Poor do
use Included
@callback fn(model) -> %{ model | poor: true } end
end
毫不奇怪,我有一个invalid quoted expression: #Function<0.100338 in file:web/models/user.ex>。我发现 Jose Valim 提到不可能取消引用匿名函数。
那么,您能介绍一下在 elixir 中将自定义代码传递和调用到宏中的好方法吗?
【问题讨论】: