// 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 );
Copyright © 2017-2019, Tony J. Hudgins
MIT
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.