You can use Kernel.defguard/1
(or defguardp/1
) to do this now:
defmodule Player do
defstruct [:name, cards: []]
defguard has_cards(cards) when cards != []
def play_card(%__MODULE__{cards: cards}) when has_cards(cards), do: []
def play_card(_player), do: []
end
Documentation: https://hexdocs.pm/elixir/Kernel.html#defguard/1
However, guards of necessity are still very simple constructs, so you can't really put complex logic in them. For reference, here's what you can put in guards: https://hexdocs.pm/elixir/master/guards.html#list-of-allowed-expressions