September 04, 2015 My problem with const | ||||
---|---|---|---|---|
| ||||
I love the way D does transitive const and immutable but i have a problem when i use const a struct field. --- struct MyStruct { int a; const int b; } struct Range { MyStruct front; void popFront() { front = MyStruct(2, 3); } enum empty = false; } --- This sample doesn't compile. |
September 04, 2015 Re: My problem with const | ||||
---|---|---|---|---|
| ||||
Posted in reply to Freddy | On Friday, 4 September 2015 at 23:26:39 UTC, Freddy wrote:
> This sample doesn't compile.
That's due to the nature of structs - you would be overwriting a const field.
Const fields should probably be avoided since they aren't all that useful. You could instead do a const getter property for it without a corresponding setter, then make the actual data private.
|
Copyright © 1999-2021 by the D Language Foundation