RfC for language feature: rvalue struct

FeepingCreature feepingcreature at gmail.com
Wed Feb 1 07:46:24 UTC 2023


On Tuesday, 31 January 2023 at 18:13:58 UTC, Ola Fosheim Grøstad 
wrote:
> On Tuesday, 31 January 2023 at 13:02:57 UTC, FeepingCreature 
> wrote:
>> Okay, now how do you do that without causing GC load? :) Keep 
>> in mind that we plan to use this for very inner loops of 
>> not-always-cheap algorithms.
>
> Why can't you use regular encapsulation?
>
> I think it would be better for you to see if you can do this is 
> as a rewrite instead as that doesn't affect the type system.
>
> E.g. C# introduced "record" in addition to "struct" and "class" 
> to get slightly different value semantics, but as far as I can 
> tell it is only a rewrite.

Maybe it can be done as a rewrite?

For instance, we used to do

```
struct S
{
     private int a_;

     public int a() { return this.a_; }
}
```

But this is not quite the semantic we actually want because, for 
instance, the accessors will still run the invariants, which is 
pointless because we know the fields won't ever change after the 
constructor call, and also the function call itself is just 
useless. But that's the interface we want, at least.

The thing we *actually* want is, roughly,

```
struct S
{
     private int a_;

     public rvalue alias a = this.a_;
}
```

But then we're inventing features again.

That said, even "a function that returns 'a'" isn't *quite* the 
right semantics, because this is valid D:

```
struct S { int a; }
S foo() { ... }
foo.a = 5;
```

On that topic, what the fuck, D.


More information about the Digitalmars-d mailing list