Norm.Contract (Norm v0.13.0) View Source

Design by Contract with Norm.

This module provides a @contract macro that can be used to define specs for arguments and the return value of a given function.

To use contracts, call use Norm which also imports all Norm functions.

Sometimes you may want to turn off contracts checking. For example, to skip contracts in production, set: config :norm, enable_contracts: Mix.env != :prod.

Examples

defmodule Colors do
  use Norm

  def rgb(), do: spec(is_integer() and &(&1 in 0..255))

  def hex(), do: spec(is_binary() and &String.starts_with?(&1, "#"))

  @contract rgb_to_hex(r :: rgb(), g :: rgb(), b :: rgb()) :: hex()
  def rgb_to_hex(r, g, b) do
    # ...
  end
end