我需要这样的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
并将每个调用转换为规则宏(我已经定义)并让它在SomeModule
模块中定义一个函数。像这样:
def is_north_america_rule(Address[continent: "North America"] = address) do
# do something
end
我想对传递给宏生成的函数的参数使用模式匹配。我已经看到如何在宏中定义自定义函数,但我不确定如何在宏生成的函数中实现模式匹配和保护。
提前致谢!