Immutable fields

Daniel Murphy yebblies at nospamgmail.com
Tue Nov 2 20:52:32 PDT 2010


"bearophile" <bearophileHUGS at lycos.com> wrote in message 
news:iaqgvl$1qbp$1 at digitalmars.com...
> So do you think my code shows a compiler bug?

I'd like immutable to be implicitly static in some cases eg.

void foo()
{
    immutable int[20] table = [...];
}

If this is possible, it does reduce the storage required and repeated init 
times.  (Although it seems to be using enum, not static, which has its own 
set of problems)

Removing the storage inside structs completely scews up the memory layout 
(which is meant to be C compatible)

The fact that commenting out the constructor in struct C changes the size of 
the struct seems like a bug to me, or at least a horribly messy part of the 
spec.

struct A
{
    int x;
}
struct B
{
    immutable int x;
}
struct C
{
    immutable int x;
    this(int a) { x = a; }
}

Also, this seems to work for me.


enum E { A, B };

struct Foo {
    immutable E x;
 this(int dummy) { x = E.A; }
}

struct Bar {
    immutable E x;
 this(int dummy) { x = E.B; }
}

void main() {

 auto f = Foo(0);
 assert((cast(Bar*)&f).x == E.A);
}


Also, What the hell?




More information about the Digitalmars-d mailing list