Const struct syntax

Jonathan M Davis jmdavisProg at gmx.com
Tue Aug 30 04:19:12 PDT 2011


On Tuesday, August 30, 2011 07:08:32 bearophile wrote:
> DMD 2.055head gives no compile-time errors on this, is this expected and
> good/acceptable?
> 
> 
> struct Foo {
>     int x;
>     this(int x_) { this.x = x_; }
> }
> void main() {
>     auto f1 = new const(Foo)(1);
>     f1.x++;
>     auto f2 = new immutable(Foo)(1);
>     f2.x++;
>     auto f3 = const(Foo)(1);
>     f3.x++;
>     auto f4 = immutable(Foo)(1);
>     f4.x++;
> }

I believe that that's as expected. If the struct had member variables which 
were references or pointers, then you'd need an immutable constructor to 
construct an immutable one, but in this case, it shouldn't be necessary. And I 
don't think that it's ever necessary to have a special constructor for const.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list