pattern match with struct
- Structでもpattern matchingすることができる
- [Kernel] Allow variable struct names when matching, for example, %module{key: “value”} = struct
__struct__
__struct__ が補完候補に出てこない。けれど、使うことは可能。
- Elixir 1.2.6
iex(1)> Version. InvalidRequirementError InvalidVersionError Parser Requirement __struct__/0 compare/2 match?/2 parse/1
- Elixir 1.3.1
iex(1)> Version. InvalidRequirementError InvalidVersionError Parser Requirement compare/2 compile_requirement/1 match?/2 match?/3 parse!/1 parse/1 parse_requirement/1
ただし、Elixir1.3.1でも、
iex(1)> Version.__struct__
%Inspect.Error{message: "got Protocol.UndefinedError with message \"protocol Enumerable not implemented for nil\" while inspecting %{__struct__: Version, build: nil, major: nil, minor: nil, patch: nil, pre: nil}"}
iex(1)> %Version{}
%Inspect.Error{message: "got Protocol.UndefinedError with message \"protocol Enumerable not implemented for nil\" while inspecting %{__struct__: Version, build: nil, major: nil, minor: nil, patch: nil, pre: nil}"}
と使うことは可能。