www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - const struct not accepted as initializer by dmd 2.002

reply torhu <fake address.dude> writes:
Reposting from D.learn, since there were no answers there.

---
struct Inner
{
     int value;
}

struct Outer {
     //const(Inner) i;  // this fixes it, but is not valid in D 1.0
     Inner i;
}

const Inner ic = { 5 };  // const or invariant here makes no difference

Outer outer = {
     i: ic      // line 14
     //i: { 5 }  // this is allowed
};
---
c:\prog\test\D>dmd -c test
test.d(14): Error: non-constant expression cast(Inner)ic
---

Since the value of ic is fixed at compile time, shouldn't it be 
equivalent to a literal?

I'm trying to make this code work in D version 1 too, and since it's for 
an example, I'm trying to avoid any string mixins or other ugly workarounds.
Jul 13 2007
next sibling parent reply Regan Heath <regan netmail.co.nz> writes:
torhu wrote:
 Reposting from D.learn, since there were no answers there.
 
 ---
 struct Inner
 {
     int value;
 }
 
 struct Outer {
     //const(Inner) i;  // this fixes it, but is not valid in D 1.0
     Inner i;
 }
 
 const Inner ic = { 5 };  // const or invariant here makes no difference
 
 Outer outer = {
     i: ic      // line 14
     //i: { 5 }  // this is allowed
 };
 ---
 c:\prog\test\D>dmd -c test
 test.d(14): Error: non-constant expression cast(Inner)ic
 ---
 
 Since the value of ic is fixed at compile time, shouldn't it be 
 equivalent to a literal?
 
 I'm trying to make this code work in D version 1 too, and since it's for 
 an example, I'm trying to avoid any string mixins or other ugly 
 workarounds.
I can't think of anything. Is there a pre-defined version specifier for D1.0 and D2.0 you could use to conditionally select code which works for each? Regan
Jul 13 2007
next sibling parent torhu <fake address.dude> writes:
Regan Heath wrote:
 torhu wrote:
 Reposting from D.learn, since there were no answers there.
 
 ---
 struct Inner
 {
     int value;
 }
 
 struct Outer {
     //const(Inner) i;  // this fixes it, but is not valid in D 1.0
     Inner i;
 }
 
 const Inner ic = { 5 };  // const or invariant here makes no difference
 
 Outer outer = {
     i: ic      // line 14
     //i: { 5 }  // this is allowed
 };
 ---
 c:\prog\test\D>dmd -c test
 test.d(14): Error: non-constant expression cast(Inner)ic
 ---
 
 Since the value of ic is fixed at compile time, shouldn't it be 
 equivalent to a literal?
 
 I'm trying to make this code work in D version 1 too, and since it's for 
 an example, I'm trying to avoid any string mixins or other ugly 
 workarounds.
I can't think of anything. Is there a pre-defined version specifier for D1.0 and D2.0 you could use to conditionally select code which works for each?
The are several simple workarounds. I guess my real question is if this not working is a bug or not. Shouldn't a struct value that's const or invariant be equal to a literal? The value of 'ic' is known at compile-time. Isn't that enough? And can anyone explain this? If I replace this --- struct Outer { Inner i; } --- with this --- struct Outer { const(Inner) i; } --- then i'm actually allowed to initialize outer's i with the const ic variable. That seems a bit backwards, since I've actually changed i from non-const to const.
Jul 13 2007
prev sibling parent "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"Regan Heath" <regan netmail.co.nz> wrote in message 
news:f77ov6$1hem$1 digitalmars.com...
 torhu wrote:
 Reposting from D.learn, since there were no answers there.

 ---
 struct Inner
 {
     int value;
 }

 struct Outer {
     //const(Inner) i;  // this fixes it, but is not valid in D 1.0
     Inner i;
 }

 const Inner ic = { 5 };  // const or invariant here makes no difference

 Outer outer = {
     i: ic      // line 14
     //i: { 5 }  // this is allowed
 };
 ---
 c:\prog\test\D>dmd -c test
 test.d(14): Error: non-constant expression cast(Inner)ic
 ---

 Since the value of ic is fixed at compile time, shouldn't it be 
 equivalent to a literal?

 I'm trying to make this code work in D version 1 too, and since it's for 
 an example, I'm trying to avoid any string mixins or other ugly 
 workarounds.
I can't think of anything. Is there a pre-defined version specifier for D1.0 and D2.0 you could use to conditionally select code which works for each?
There's D_Version2. But there's still a problem - code that is CC'd out still has to be syntactically valid. You'd have to put the code that relies on 2.0 syntax in a separate, conditionally imported module. Stewart.
Jul 13 2007
prev sibling parent torhu <no spam.invalid> writes:
torhu wrote:
 Reposting from D.learn, since there were no answers there.
 
 ---
 struct Inner
 {
      int value;
 }
 
 struct Outer {
      //const(Inner) i;  // this fixes it, but is not valid in D 1.0
      Inner i;
 }
 
 const Inner ic = { 5 };  // const or invariant here makes no difference
 
 Outer outer = {
      i: ic      // line 14
      //i: { 5 }  // this is allowed
 };
 ---
 c:\prog\test\D>dmd -c test
 test.d(14): Error: non-constant expression cast(Inner)ic
 ---
 
 Since the value of ic is fixed at compile time, shouldn't it be 
 equivalent to a literal?
Jul 14 2007