Record

Immutable value type that automatically implements equality (==, !=), hashcode computation (toHash), and stringification (toString). The purpose of this struct is act similarly to record types in functional programming languages like OCaml and Haskell.

Constructors

this
this(Types values)

Accepts parameters matching the types of the fields declared in the template arguments and automatically assigns values to the backing fields.

Members

Functions

deconstruct
void deconstruct(staticMap!(toPointer, Types) ptrs)

Deconstruction support for the <a href="/dext/dext/let">let module</a>.

opEquals
bool opEquals(auto ref const Self other)

Implements equality comparison with other records of the same type. Two records are only considered equal if all fields are equal.

toHash
size_t toHash()

Computes the hashcode of this record based on the hashes of the fields, as well as the field names to avoid collisions with other records with the same number and type of fields.

toString
string toString()

Stringifies and formats all fields.

Examples

1 // define a point int 2D space
2 alias Point = Record!(
3     int, "x",
4     int, "y"
5 );
6 
7 auto a = Point( 3, 7 );
8 auto b = Point( 9, 6 );
9 
10 // Euclidean distance
11 auto distance( in Point a, in Point b )
12 {
13     import std.math : sqrt;
14 
15     return sqrt( ( a.x - b.x ) ^^ 2f + ( a.y - b.y ) ^^ 2f );
16 }
17 
18 auto dist = distance( a, b ); // 6.08276

Meta

Authors

Tony J. Hudgins