I am currently stuck in implementing a macro Here’s the code I currently have (without unnecessary code):
defmacro request(opts \\ [], do: expr) do
quote
typedstruct unquote(opts) do
unquote(expr)
end
end
end
That macro works fine, but I would like to add some options that cannot be overwritten, so I tried this:
defmacro request(opts \\ [], do: expr) do
quote
typedstruct Keyword.put(unquote(opts), :module, Req) do
unquote(expr)
end
end
end
Sadly, this fails with a compilation error:
** (FunctionClauseError) no function clause matching in Access.get/3
The following arguments were given to Access.get/3:
# 1
{{:., [line: 6], [{:__aliases__, [line: 6, counter: {ChangeUser, 8}, alias: false], [:Keyword]}, :put]}, [line: 6], [[], :module, {:__aliases__, [line: 6, counter: {ChangeUser, 8}, alias: false], [:Req]}]}
# 2
:enforce
# 3
nil
Attempted function clauses (showing 6 out of 6):
def get(%module{} = container, key, default)
def get(map, key, default) when is_map(map)
def get(list, key, default) when is_list(list) and is_atom(key)
def get(list, key, _default) when is_list(list) and is_integer(key)
def get(list, key, _default) when is_list(list)
def get(nil, _key, default)
That error occurs, because an AST is passed to the typedstruct
-macro instead of a keyword list and typed_struct then tries to call opts[:enforce]
(tuples do not implement the Access
-behaviour).
Is there anyone who can help me figure out what is happening and how to solve this issue?