Thread overview
new definition of static?
Jul 19, 2004
Sean Kelly
Jul 19, 2004
Kris
Jul 19, 2004
Sean Kelly
Jul 19, 2004
Kris
Aug 15, 2004
Walter
July 19, 2004
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


July 19, 2004
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
>
>


July 19, 2004
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


July 19, 2004
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
>
>


August 15, 2004
"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();