Struct initializers and const in 2.009

torhu no at spam.invalid
Sun Jan 6 13:16:26 PST 2008


Another surprise I got was when doing something like this:

---
struct Bar {
    int f;
}

const Bar bar = { 5 };

void f(in Bar b)
{
    Bar[3] array;
    array[0] = b;   // line 10
    //array[0] = *cast(Bar*)&b;  // ugly workaround
}


void main()
{
    f(bar);
}
---
bug2.d(10): Error: cannot implicitly convert expression (b) of type 
const(Bar) to Bar

The problem here is that f's parameter has to be 'in' or 'const', 
otherwise bar can't be used as an argument to f.  But a const Bar can't 
be assigned to an element of an array, since the elements cannot be 
const, or assigning to them would be disallowed.

So I guess mixing const and mutable structs is no longer viable as of 
2.009, they would have to be separate types.  But I guess that's the way 
transitive const has to be.  In this example, making bar an enum instead 
of a const would solve the issue, though.  I'm only doing this because 
I'm looking for an 1.0 compatible way of declaring struct consts like 
bar.  Maybe I should just remove const instead.

Trying to cast away the constness of f's b argument doesn't work either. 
  The compiler complains about a missing opCall when I do 'cast(Bar)b'. 
  Is that bug, or is a different syntax for casting away const needed?



More information about the Digitalmars-d mailing list