Record

A mixin template for turning a struct into an immutable value type that automatically implements equality (==, !=), hashcode computation (toHash), stringification (toString), and mutation methods that create new copies with new values.

The purpose of this struct is act similarly to record types in functional programming languages like OCaml and Haskell.

Accepts an optional, boolean template parameter. When true, the mixin will generate a deconstruction method for use with <a href="/dext/dext/let">let</a>. Default is false.

All fields on the struct must start with an underscore and be non-public. Both are enforced with static asserts.

Constructors

this
this(FieldTypeTuple!(typeof(this)) args)
Undocumented in source.

Members

Functions

deconstruct
void deconstruct(staticMap!(asPointer, FieldTypeTuple!(typeof(this))) ptrs)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(typeof(this) other)
Undocumented in source. Be warned that the author may not have intended to support it.
toHash
size_t toHash()
Undocumented in source. Be warned that the author may not have intended to support it.
toHash
size_t toHash()
Undocumented in source. Be warned that the author may not have intended to support it.
toString
string toString()
Undocumented in source. Be warned that the author may not have intended to support it.
toString
string toString()
Undocumented in source. Be warned that the author may not have intended to support it.

Examples

// define a point int 2D space
struct Point
{
    mixin Record!true;
    private int _x, _y;
}

auto a = Point( 3, 7 );
auto b = a.withX( 10 ); // creates a new copy of 'a' with a new 'x' value of 10 and the same 'y' value such that b == Point( 10, 7 )

// Euclidean distance
auto distance( in Point a, in Point b )
{
    import std.math : sqrt;

    return sqrt( ( a.x - b.x ) ^^ 2f + ( a.y - b.y ) ^^ 2f );
}

auto dist = distance( a, b );

Meta

Authors

Tony J. Hudgins