deep copying a struct
Paul Backus
snarwin at gmail.com
Fri Sep 6 16:41:30 UTC 2019
On Friday, 6 September 2019 at 10:52:43 UTC, aliak wrote:
> On Friday, 6 September 2019 at 10:37:16 UTC, aliak wrote:
>> Are there any library APIs that allow this:
>
> I just put this together. Any holes other the AA related ones?
string a = "hello";
string b = a.dupDeep;
// Error: cannot implicitly convert expression dupDeep(a)
// of type dchar[] to string
Using `ElementType` means you get bitten by autodecoding. Since
you know it's specifically an array, and not just any range, you
can use `typeof(thing[0])` instead.
---
struct S { immutable int i; }
S a = S(123);
S b = a.dupDeep;
// Error: cannot modify struct instance `copy` of type `S`
// because it contains `const` or `immutable` members
I think the only way to make this work in the general case is
with cooperation from the types, since the only way to initialize
an `immutable` member is in a constructor. If you don't care
about that, though, you can just slap an `if
(isAssignable!(Unqual!T))` template constraint on the whole thing.
More information about the Digitalmars-d-learn
mailing list