Skip to content

Commit

Permalink
Add record.create, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
hmmdyl committed Jul 15, 2021
1 parent 9cb277d commit 80e8e21
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 10 additions & 0 deletions source/drecord.d
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 80e8e21

Please sign in to comment.