RfC for language feature: rvalue struct
Salih Dincer
salihdb at hotmail.com
Mon Jan 30 03:09:14 UTC 2023
On Sunday, 29 January 2023 at 20:48:11 UTC, FeepingCreature wrote:
>
> This should emphatically *not* work. You're mutating a field of
> `test1`. That's what we don't want to ever happen!
>
> Instead, you should have to write `test1 = test1(test1.data ~
> "234");`
>
> This looks extremely similar, but the key is that the
> assignment has to go through the constructor. That way, `test1`
> can only go from "valid value" to "valid value".
In order to use it as you say, the following opCall() should be
implemented:
```d
this(R)(R[] data) { opCall(data); }
alias opCall this;
@property opCall() inout { return data; }
@property opCall(R)(R[] data)
{
return this.data = data.to!(ImmutableOf!T[]);
}
```
Then it works like this:
```d
immutable arr = [ 1, 2, 3];
auto num1 = S!int(arr.dup);
auto num2 = S!int(arr);
assert(is(typeof(num1) == S!int));
assert(is(typeof(num1.data) == immutable(int)[]));
num1(num1 ~ 4); // or:
num1 = num1 ~ 5;
assert(num1.length == 5);
assert(num1.length > num2.length);
num2 = num1;
assert(num1.length == num2.length);
```
But `num1 ~= 6` doesn't work because it doesn't have an overload:
> Error: cannot append type `int` to type `S!int`
SDB at 79
More information about the Digitalmars-d
mailing list