RfC for language feature: rvalue struct
Salih Dincer
salihdb at hotmail.com
Sat Jan 28 19:35:49 UTC 2023
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
More information about the Digitalmars-d
mailing list