2017-11-14 15 views
1

マクロを見ていただけますか? 私は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 

答えて

2

あなたはunquotenumberする必要はありません。 は、quoteブロックの外にある変数を注入する場合に使用されます。 numberquoteの内部に定義されているため、unquoteは不要です。以下はあなたのために働くはずです:

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

ありがとうございます。どのように私はそれを逃したか分からない。乾杯 –

関連する問題