cannot modify struct with immutable members

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 2 21:55:34 PST 2015


On Sat, 03 Jan 2015 15:56:58 +1030
ted via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:

> Ironically, I'm trying to use const in an effort to understand it...but 
> there seems to be an unusual amount of pain until I grok it.
just remember that `const` "infects" everything down to the bytes when
it's applied. and it's forbidden to overwrite `const` vars with
different values.

in your canse it "infects" your `A` struct, effectively converting it
to `const A` (with `const int someInt`). and as you can't change
"consted" value, and structs are `memcpy()`ed... compiler tracked that
down and refused to do it.

so you don't really need `const` fields in D. you can make getters
`const` though, so that they can be used on `const MyStruct` instances.

so two rules should help you here:
1. it's forbidden to overwrite `const` vars with new values.
2. `const` will infect everything down to bytes.

maybe this will help you like it helps me. ;-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150103/93288e3f/attachment.sig>


More information about the Digitalmars-d-learn mailing list