User-defined data types Mapping

User-defined data types Mapping

The constructors of Sum/Product algebraic data types are mapping to tuples in Erlang at compile-time.

Hamler:

-- Sum datatype
data Color = Red | Green | Blue
Red   -- {'Red'}
Green -- {'Green'}
Blue  -- {'Blue'}

-- Product datatype
data Pair = Pair Integer Integer
Pair 3 4 -- {'Pair',3,4}

-- Maybe
data Maybe a = Just a | Nothing
Nothing -- {'Nothing'}
Just 5  -- {'Just',5}

Erlang:

{'Red'}   %% Red
{'Green'} %% Green
{'Blue'}  %% Blue

{'Pair',3,4} %% Pair 3 4

{'Nothing'} %% Nothing
{'Just',5}  %% Just 5