Module similar to Record, meant as Tuple extension.
Record-like API
This module provides deftuple/2 and deftuplep/2 macros similar to
Record.defrecord/3 and Record.defrecordp/3 macros. The only
difference is that deftuple/2 and deftuplep/2 do not introduce
a tag (atom) as the first element in a resulting tuple data.
The advantage of having alternate API (featured with named tuple elements) when comparing to literal syntax of tuples becomes apparent when a tuple has more elements and/or its arity or structure changes over time in a complex code.
Defining such "tag-less records" may seem odd, but it has at least one notable use case: in ETS table to store tuples of different shapes where the key typically consists of a fixed tag (to identify shape) and variable part(s) (to differentiate instances). In such heterogenic ETS tables there's also a place for use of records when singular instances are appropriate (e.g. to hold "global" counters).
Summary
Functions
Defines a set of macros to create, access, and pattern match on a tuple.
Same as deftuple/2 but generates private macros.
Functions
Defines a set of macros to create, access, and pattern match on a tuple.
The name of the generated macros will be name (which has to be an
atom). kv is a keyword list of name: default_value fields for the
new tuple.
The following macros are generated:
name/0to create a new tuple with default values for all fieldsname/1to create a new tuple with the given fields and values, to get the zero-based index of the given field in a tuple or to convert the given tuple to a keyword listname/2to update an existing tuple with the given fields and values or to access a given field in a given tuple
All these macros are public macros (as defined by defmacro).
See the "Examples" section for examples on how to use these macros.
Examples
defmodule Space do
require Tuple
Tuple.deftuple :point, [x: 0, y: 0, z: 0]
endIn the example above, a set of macros named point but with different
arities will be defined to manipulate the underlying tuple.
# Import the module to make the point macros locally available
import Space
# To create tuples
tuple = point() #=> {0, 0, 0}
tuple = point(x: 7) #=> {7, 0, 0}
# To get a field from the tuple
point(tuple, :x) #=> 7
# To update the tuple
point(tuple, y: 9) #=> {7, 9, 0}
# To get the zero-based index of the field in tuple
point(:x) #=> 0
# Convert a tuple to a keyword list
point(tuple) #=> [x: 7, y: 0, z: 0]The generated macros can also be used in order to pattern match on tuples and to bind variables during the match:
point() = tuple #=> {7, 0, 0}
point(x: x) = tuple
x #=> 7
Same as deftuple/2 but generates private macros.