On Thursday, September 08, 2011 05:33:01 bearophile wrote: > With the latest DMD versions it's easy to create an immutable array of > structs, with a pure function (I think this gives advantages similar to the > Transients of the Clojure language): [snip]
My take on it is pretty much just not a good idea create structs with const or immutable fields. Once they're const or immutable, you can't ever assign to them again, and as you point out, it really doesn't work to put them in arrays. Structs are generally meant to be assignable - and the fact that it's so easy to end up in a situation where a variable needs to be the struct's init property first, it's that much critical. Giving them const or immutable fields tends to mess with that pretty thoroughly. I'd argue that if you want a field in a struct to be treated as const or immutable, then you should make it private and give it a getter property function which returns a const or immutable version of it (and it can even return it by ref if you want to avoid unnecessary copying). So, yes. The situation is a bit of a pain, but init pretty much fries any chance of making const or immutable struct fields easy to deal with just like it fries the ability to have default constructors for structs. - Jonathan M Davis
