diff --git a/readme.md b/readme.md index e676a7e..6f47995 100644 --- a/readme.md +++ b/readme.md @@ -64,6 +64,9 @@ alias DefaultRecord = record!( auto r = new DefaultRecord; // run the default initialisers writeln(r); // {x = 4, o = object.Object} + +auto q = DefaultRecord.create!"x"(9); // run default initialisers, then set x to 9 +writeln(r); // {x = 9, o = object.Object} ``` [0]: [Records in C#](https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/types/records) \ No newline at end of file diff --git a/source/drecord.d b/source/drecord.d index 55bbca1..fe480d7 100644 --- a/source/drecord.d +++ b/source/drecord.d @@ -158,6 +158,16 @@ template record(args...) mixin(genCtor); + /// Explicitly set certain fields, default initialise the rest + static record create(TNames...)(...) + { + auto r = new record; + import core.vararg; + static foreach(item; AliasSeq!TNames) + mixin("r." ~ item ~ "_ = va_arg!(typeof(" ~ item ~ "_))(_argptr);"); + return r; + } + /++ Test for equality. Reference types are checked to ensure + their references are the same (point to same thing), value types + are checked to ensure their values are identical.