Question

In Elixir, how would I go about creating a new guard clause for a function? Obviously, I've seen that you can't just call any function in a when statement, but it would be nice to be able to do something like this:

defmodule Player do
  def play_card(player), do: []
  def play_card(player) when has_cards(player), do: ...
  # Define has_cards as guard clause?
end
Was it helpful?

Solution 2

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

OTHER TIPS

The short answer is: you can't.

The long answer is that you can define macros and they will work in guards as long as they are made of valid guards expressions. But that is non-trivial for complex guards so it is better avoided unless you really have to.

The list of valid guards can be found in the getting started guide: http://elixir-lang.org/getting-started/case-cond-and-if.html#expressions-in-guard-clauses

A sample implementation of a "complex" guard macro can be found in Elixir source: https://github.com/elixir-lang/elixir/blob/5d34fcdccd20bf38c4f2a13f8adeb78e22b4e74c/lib/elixir/lib/kernel.ex#L1574

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top