difficulties with const structs and alias this / template functions
Stanislav Blinov
stanislav.blinov at gmail.com
Mon Nov 19 01:24:02 UTC 2018
On Monday, 19 November 2018 at 00:50:28 UTC, Dennis wrote:
> I'm also trying to make it work with immutable, and from BigInt
> [2] I learned that constructors need to be `pure` for creating
> immutable objects. (I don't know why.)
That's only for types with indirections (pointers), since `pure`
guarantees that you do not mutate any global state.
For value types, they work just fine:
struct q16 {
uint x;
this(uint x) { this.x = x; }
this(uint x) const { this.x = x; }
this(uint x) immutable { this.x = x; }
// or again, all three packed together:
this(this T)(uint x) { this.x = x; }
}
> Are there any other gotchas? I didn't add an immutable variant
> for toQ32 like Stanislav suggested, but creating an immutable
> q32 from a q16 still seems to work fine.
Yup, that's because, like Rubn said, copying value types is
trivial. Where it all comes to bite you is when you start having
pointers, because you can't copy a const(T)* into a T*.
More information about the Digitalmars-d-learn
mailing list