让我说我有:
defmodule Operator do
defstruct operator: nil
@type t :: %Operator {
operator: oper
}
@type oper :: logic | arithmetic | nil
@type logic :: :or | :and
@type arithmetic :: :add | :mul
end
然后我可以:
o = %Operator{operator: :and}
是否可以检查o.operator
是logic
,arithmetic
还是nil
?
答案 0 :(得分:8)
def operator(%Operator{operator: op}) when op in [:or, :and, :add, :mul, nil] do
...
end
或者:
@ops [:or, :and, :add, :mul, nil]
def operator(%Operator{operator: op}) when op in @ops do
...
end