Function Elixir使用模式匹配构建自定义函数,并使用宏进行保护?

Function Elixir使用模式匹配构建自定义函数,并使用宏进行保护?,function,macros,erlang,dsl,elixir,Function,Macros,Erlang,Dsl,Elixir,我需要这样的DSL: defmodule SomeModule do use SomeMacros # sample shipping DSL rule is_north_america do calculate_shipping_cost_with usps end rule is_north_america and november_or_december do calculate_shipping_cost_with ups end rul

我需要这样的DSL:

defmodule SomeModule do
  use SomeMacros

  # sample shipping DSL
  rule is_north_america do
    calculate_shipping_cost_with usps
  end

  rule is_north_america and november_or_december do
    calculate_shipping_cost_with ups
  end

  rule is_south_america do
    calculate_shipping_cost_with fedex
  end

  rule is_somewhere_else do
    calculate_shipping_cost_with dhl
  end
end
并将每个调用转换为rule宏(我已经定义了),让它在
SomeModule
module中定义一个函数。像这样:

def is_north_america_rule(Address[continent: "North America"] = address) do
  # do something
end 
我希望对传递到宏生成的函数中的参数使用模式匹配。我见过如何在宏中定义自定义函数,但我不确定如何在宏生成的函数中实现模式匹配和保护


提前谢谢

下面是一个例子,说明如何实现类似的目标:


我本打算补充更多信息,但我决定提供一个简单的例子,然后回答您可能提出的任何进一步的问题。不过需要注意的是,尽量简化DSL规则,如果您看到宏代码开始变得越来越复杂,简单地使用
cond
(显式)会更好。

非常好的说明!要点确实把一切都讲清楚了。我真的很感谢你在过去几周里给我的所有详细答案。这段代码不再工作了,我得到了这个错误:expected->子句for do in cond,有什么想法吗?升级到Elixir v1.0。