Guardsafe

Build StatusInline docsHex.pm

Macros expanding into code that can be safely used in guard clauses.

Usage

Update your mix.exs file and run mix deps.get.

defp deps do
  [{:guardsafe, "~> 0.3.0"}]
end

Import all the macros...

defmodule MyModule do
  import Guardsafe

...or just the ones you need.

defmodule MyOtherModule do
  import Guardsafe, only: [divisible_by?: 2, integer?: 1]

Now go forth and make your guard clauses more readable!

def leap_year?(year) when not integer?(year), do: raise "That's not a proper year!"
def leap_year?(year) when divisible_by?(year, 400), do: true
def leap_year?(year) when divisible_by?(year, 100), do: false
def leap_year?(year), do: divisible_by?(year, 4)

Documentation for each macro is of course available in iex.

iex(1)> h Guardsafe.divisible_by?

                    defmacro divisible_by?(number, divisor)

Expands divisible_by?(number, divisor) into rem(number, divisor) == 0

Available macros

NB: If a macro is translated into a function residing in another module than Kernel you need to require it before use, e.g. require Integer.

Expressive calculations

Macros for checking types

Macros for checking values

Why nil? and float? instead of is_nil and is_float

While the Elixir core team thinks that nil? compared to is_nil is an inconcistency, others, especially Rubyists, are more comfortable with the nil? notation.

Online documentation

For more information, see the full documentation.