【问题标题】:Elixir Macro issue with unquoting取消引用的 Elixir 宏问题
【发布时间】:2017-11-15 08:04:09
【问题描述】:

你能看看我的宏吗? 我收到undefined function number/0 错误,我不知道为什么。

 defmodule DbUtil do
        defmacro __using__(opts) do
            quote do
              import unquote(__MODULE__)
              @before_compile unquote(__MODULE__)
            end
        end

        defmacro __before_compile__(%{module: definition} = _env) do

            quote do
                import Ecto.Query

                def last do
                    from x in unquote(definition), order_by: [desc: x.id], limit: 1
                end

                # This dumps error
                def limits(number) do
                    from a in unquote(definition), limit: ^unquote(number)
                end
            end

        end
    end

【问题讨论】:

    标签: macros elixir


    【解决方案1】:

    你不需要unquotenumberunquote 用于当您想要注入存在于 quote 块之外的变量时。由于number 是在quote 中定义的,因此您不需要unquote。以下内容应该适合您:

    def limits(number) do
      from a in unquote(definition), limit: ^number
    end
    

    【讨论】:

    • 非常感谢。不知道我是怎么错过的。干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多