Elixir Guards

From Elixir Wiki
Jump to navigation Jump to search

Elixir Guards[edit]

File:Elixir logo.png
Elixir Logo

Elixir Guards are a powerful feature in the Elixir programming language that allow developers to specify conditions on function parameters. Guards are used to ensure that a function is only executed when specific conditions are met. In other words, guards can be used to pattern match against the values of function arguments and apply different behaviors based on those values.

Syntax[edit]

In Elixir, guards are defined using the `when` keyword followed by one or more expressions. The expressions can be any valid Elixir code that produce a value. Guards are typically used in function definitions using the `def` keyword.

Here is an example of a function definition with a guard:

```elixir defmodule Math do

 def double(x) when is_number(x) do
   x * 2
 end

end ```

In this example, the `double/1` function is defined with a guard that checks if the argument `x` is a number using the `is_number/1` built-in function. If the guard condition is satisfied, the function multiplies `x` by 2 and returns the result.

Available Guards[edit]

Elixir provides several built-in guards that can be used in function definitions. These guards include:

  • `is_atom/1`: Checks if the given term is an atom.
  • `is_binary/1`: Checks if the given term is a binary.
  • `is_boolean/1`: Checks if the given term is a boolean (`true` or `false`).
  • `is_function/1`: Checks if the given term is a function.
  • `is_integer/1`: Checks if the given term is an integer.
  • `is_list/1`: Checks if the given term is a list.
  • `is_map/1`: Checks if the given term is a map.
  • `is_number/1`: Checks if the given term is a number.
  • `is_pid/1`: Checks if the given term is a process identifier.
  • `is_port/1`: Checks if the given term is a port.
  • `is_reference/1`: Checks if the given term is a reference.
  • `is_tuple/1`: Checks if the given term is a tuple.

These are just some of the available guards in Elixir. Guards can also be combined using logical operators such as `and`, `or`, and `not` to create more complex conditions.

Custom Guards[edit]

In addition to the built-in guards, Elixir also allows developers to define their own custom guards. Custom guards are implemented as functions returning a boolean value and can be used in function definitions.

Here is an example of a function definition with a custom guard:

```elixir defmodule Math do

 def even(x) when rem(x, 2) == 0 do
   true
 end

end ```

In this example, the `even/1` function is defined with a custom guard that checks if the argument `x` is an even number by using the `rem/2` function to calculate the remainder when dividing `x` by 2. If the remainder is equal to 0, the guard returns `true`, indicating that the number is indeed even.

Custom guards can greatly enhance the expressiveness and readability of Elixir code, allowing developers to create functions with specialized conditions tailored to their specific needs.

Conclusion[edit]

Elixir guards provide a powerful mechanism for pattern matching and conditional execution in function definitions. By using guards, developers can ensure that functions are executed only under specific conditions, leading to more robust and reliable code. With a combination of built-in guards and the ability to define custom guards, Elixir offers a flexible and expressive way to handle different cases and scenarios in programming.