www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - My problem with const

reply "Freddy" <Hexagonalstar64 gmail.com> writes:
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.
Sep 04 2015
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Sep 04 2015