record: C# like records for D
Dylan Graham
dylan.graham2000 at gmail.com
Thu Jul 15 11:52:59 UTC 2021
On Wednesday, 14 July 2021 at 23:16:05 UTC, Dylan Graham wrote:
> ... init-only-setters like in C#, wherein at the end of
> construction or duplication, the init lambda for the field is
> called, and the field can be set that once.
This has been implemented with `get_compute`. Example:
```D
alias MyRecord = record!(
get!(int, "x", () => 20),
// get_compute lets you compute a field after the rest have
been initialised
get_compute!(float, "y", (rec) => rec.x * 2f)
);
auto r = new MyRecord;
writeln(r); // {x = 20, y = 40f}
r = new MyRecord(10);
writeln(r); // {x = 10, y = 20f}
r = MyRecord.create!"x"(5);
writeln(r); // {x = 5, y = 10f}
auto q = r.duplicate!"x"(2);
writeln(q); // {x = 2, y = 4f}
```
More information about the Digitalmars-d-announce
mailing list