record: C# like records for D

Steven Schveighoffer schveiguy at gmail.com
Fri Jul 16 19:37:53 UTC 2021


On 7/16/21 10:52 AM, Dylan Graham wrote:
> On Friday, 16 July 2021 at 13:54:36 UTC, vit wrote:
>> What adventage has record over normal immutable/const class?
> 
> In terms of mutability, none.
> 
> The duplicate method, however, lets you copy and mutate (once at 
> duplication) a record without impacting the integrity of the original. 
> You can do this with an immutable class, but then you have to roll your 
> own.
> 
> Really, it's more a convenience / less typing sort of feature, as it 
> implements all the common boilerplate for you, which should help with 
> consistency of code.

What about a UFCS `duplicate` method?

I'm not doubting there are good reasons to define types this way in C#, 
but D has pretty good tools when it comes to boilerplate.

I would possibly suggest that instead of a record template that accepts 
directives using inline lambdas, etc, just accept a model type and use 
udas to adjust the record type.

i.e.:

```d
struct RecModel {
    @get_set float y;
    @get int x;
    auto getDoubleOfX() { return x * 2; }
    ... // etc
}

alias MyRecord = record!RecModel;
```

I use this kind of thing to great success in my SQL database system.

-Steve


More information about the Digitalmars-d-announce mailing list