Skip to content

vendethiel/ADT.ex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Adt

A small/light ADT module for Elixir.

Installation

Add adt to your list of dependencies in mix.exs:

def deps do
  [{:adt, "~> 1.0.0"}]
end

Usage

Definitions

You can define your ADTs this way:

defmodule MyModule do
  ADT.define foo(a: 0) | bar(val: "hey")
end

%MyModule.Foo{a: 1}

Case statement

You can use the case macro to interact with ADTs:

value = %MyModule.Bar{}

result = MyModule.case value, [
  Foo: fn(x) -> x + 1 end,
  Bar: fn(x) -> x.val <> " there" end
]

# assert result == "hey there"

The case statement can detect when you don't provide enough statements. For example the following will not expand:

value = %MyModule.Bar{}

result = MyModule.case value, [
  Foo: fn(x) -> x + 1 end
]

# This fails at build time with:
# case macro not exhaustive.\nGiven [\"Foo\"].\nPossible: [\"Bar\", \"Foo\"].

Inspecting the possible constructors

You can use the variants function to get the list of possible constructors for an ADT:

MyModule.variants

# => [MyModule.Bar, MyModule.Foo]

About

Light ADTs for Elixir

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages