Elixir Language

Behaviours

Introduction

Behaviours are a list of functions specifications that another module can implement. They are similar to interfaces in other languages.

Here’s an example behaviour:

defmodule Parser do
  @callback parse(String.t) :: any
  @callback extensions() :: [String.t]
end

And a module that implements it:

defmodule JSONParser do
  @behaviour Parser

  def parse(str), do: # ... parse JSON
  def extensions, do: ["json"]
end

The @behaviour module attribute above indicates that this module is expected to define every function defined in the Parser module. Missing functions will result in undefined behaviour function compilation errors.

Modules can have multiple @behaviour attributes.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow