【发布时间】:2020-11-25 00:22:00
【问题描述】:
使用{:typed_struct, "~> 0.2.1"} 库我有以下结构:
defmodule RequestParams do
use TypedStruct
typedstruct do
field :code, String.t()
field :name, String.t()
end
end
我正在尝试将函数参数模式匹配到结构:
def do_handle(params %RequestParams{}, _context) do
# instead of
# def do_handle(%{ "code" => code, "name" => name}, _context) do
但我得到了例外:
cannot find or invoke local params/2 inside match. Only macros can be invoked in a match and they must be defined before their invocation.
怎么了?是否有可能将函数参数与结构匹配?
【问题讨论】:
标签: struct pattern-matching elixir