self-referential invariant member

Steven Schveighoffer schveiguy at yahoo.com
Wed Feb 6 06:14:09 PST 2008


"Neil Vice" wrote
>I am attempting to provide a constant instance of a struct as a member of 
>the struct (in 2.010) and am getting unexpected compiler errors. Can 
>someone explain what the compiler thinks I'm trying to do here and/or how 
>I'd go about achieving what I want if it's possible?
>
> The following code:
>
>    struct Test
>    {
>        public invariant Test Default;
>    }
>
>    void main()
>    {
>        Test test = Test.Default;
>    }
>

Invariant members are not automatically static (though they should be).  The 
compiler is choking because it can't determine what a 'Test' is because you 
are defining it as a member of each instance.

In addition, you are going to have problems on Test test = Test.Default 
because you are not allowed to automatically copy an invariant or const 
struct to a mutable struct in case the struct has a pointer in it.  However, 
I think Walter and co. are working on getting this to work.

What you probably want to do is:

enum Test Default = {...};

as enum is now the preferred method for manifest constants.

-Steve 




More information about the Digitalmars-d-learn mailing list