On Friday, 16 July 2021 at 19:37:53 UTC, Steven Schveighoffer
wrote:
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
That is a good idea, and to be honest I haven't looked at it that
way. So the record is a separate type from its model? Ie: `class
MyRecord {}` based off `struct RecordModel {}`? Or did I
misunderstand?
With regards to things like properties and methods, do you have
RecModel inlined in the class and then forward the calls to it?
Ie:
```D
struct RecModel {
@get int x;
auto doubleOfX() { return x; }
}
class Rec {
private RecModel instance;
@property auto x() { return instance.x; }
auto doubleOfX() { return instance.doubleOfX; }
}
```
I do like the lambda directives as it, in my mind at least,
enforces the idea that the record/class must be a simple data
type (ie no crazy methods and such).
I do have to admit this was more an exercise in metaprogramming,
and since I did manage to make something I figured I'd share it.