RfC for language feature: rvalue struct

FeepingCreature feepingcreature at gmail.com
Sun Jan 29 03:29:45 UTC 2023


On Saturday, 28 January 2023 at 19:35:49 UTC, Salih Dincer wrote:
> On Thursday, 26 January 2023 at 03:39:14 UTC, FeepingCreature 
> wrote:
>>
>> The problem is that then you can't do
>>
>> ```
>> Foo foo;
>> foo = Foo(2, [3]);
>> ```
>>
>> And that may look easy to avoid, but there's a plethora of 
>> bugs where for instance Phobos does just that.
>
> No problem:
>
> ```d
> struct Foo(T)
> {
>   import std.conv   : to;
>   import std.traits : ImmutableOf;
>
>   this(R)(R bar, R[] baz)
>   {
>     this.bar = bar;
>     this.baz = baz.to!(ImmutableOf!T[]);
>   }
>
>   immutable:
>     T bar;
>     T[] baz;
> }
>
> void main()
> {
>   immutable arr = [9, 8, 7];
>   auto num1 = Foo!int(1, [2, 3, 4]);
>   auto num2 = Foo!int(1, arr);
>
>   string str = "bcd";
>   auto str1 = Foo!char('a', str.dup);
>   auto str2 = Foo!char('a', str);
> }
> ```
>
> SDB at 79

No, I mean

```
num1 = num2;
onlineapp.d(22): Error: cannot modify struct instance `num1` of 
type `Foo!int` because it contains `const` or `immutable` members
```
```


More information about the Digitalmars-d mailing list