www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - new definition of static?

reply Sean Kelly <sean f4.ca> writes:
In 0.95, this fails to compile with the error "test.d(15): non-constant
expression new E"

int main()
{
class E : Exception { this() { super( "" ); } }
static E e = new E();
return 0;
}

I had been using this technique so I only had to construct internal exceptions
once.  Is there a new method I can use to do the same thing, or is this a bug?


Sean
Jul 19 2004
next sibling parent reply "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
I'd be willing to bet that it'd work if you split the declaration from the
assignment:

static E e;
e = new E;

Sucks? Yes ...


"Sean Kelly" <sean f4.ca> wrote in message
news:cdh1j6$jto$1 digitaldaemon.com...
 In 0.95, this fails to compile with the error "test.d(15): non-constant
 expression new E"

 int main()
 {
 class E : Exception { this() { super( "" ); } }
 static E e = new E();
 return 0;
 }

 I had been using this technique so I only had to construct internal
exceptions
 once.  Is there a new method I can use to do the same thing, or is this a
bug?
 Sean
Jul 19 2004
parent reply Sean Kelly <sean f4.ca> writes:
In article <cdh2hh$ker$1 digitaldaemon.com>, Kris says...
I'd be willing to bet that it'd work if you split the declaration from the
assignment:

static E e;
e = new E;

Sucks? Yes ...
You're right, that works. How incredibly odd. But this new syntax makes it look like e will be initialized every time the function is called, which is not what I want. And I'd prefer not to have to put in a bunch of: if( !e ) e = new E(); calls... perhaps this will be fixed in 0.96? Sean
Jul 19 2004
parent "Kris" <someidiot earthlink.dot.dot.dot.net> writes:
I do this kind of thing in a static constructor; but the language/compiler
would ideally support a one-time assignment to something like a "static
final", or whatever Walter would prefer to call it.

- Kris

"Sean Kelly" <sean f4.ca> wrote in message
news:cdh3hl$kou$1 digitaldaemon.com...
 In article <cdh2hh$ker$1 digitaldaemon.com>, Kris says...
I'd be willing to bet that it'd work if you split the declaration from
the
assignment:

static E e;
e = new E;

Sucks? Yes ...
You're right, that works. How incredibly odd. But this new syntax makes
it
 look like e will be initialized every time the function is called, which
is not
 what I want.  And I'd prefer not to have to put in a bunch of:

 if( !e ) e = new E();

 calls... perhaps this will be fixed in 0.96?


 Sean
Jul 19 2004
prev sibling parent "Walter" <newshound digitalmars.com> writes:
"Sean Kelly" <sean f4.ca> wrote in message
news:cdh1j6$jto$1 digitaldaemon.com...
 In 0.95, this fails to compile with the error "test.d(15): non-constant
 expression new E"

 int main()
 {
 class E : Exception { this() { super( "" ); } }
 static E e = new E();
 return 0;
 }

 I had been using this technique so I only had to construct internal
exceptions
 once.  Is there a new method I can use to do the same thing, or is this a
bug? D doesn't do the C++ thing of wrapping dynamic initializers for local statics in a conditional. You'll need to do it manually, as in: static E e; if (!e) e = new E();
Aug 15 2004