Can the compiler catch my stupid mistake?

Jack Stouffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 14 10:36:38 PDT 2015


Recently, I made the mistake of trying to reference an enum 
pointer in a struct before it was set (see example below). I was 
wondering if it's possible for DMD to catch this mistake at 
compile time, as this currently compiles fine and segfaults on 
execution.


Thanks

----------------
enum State {
     ONE,
     TWO
}

struct TestA {
     private State *state;

     void method(ref State program_state) {
         this.state = &program_state;
     }

     this (int temp_arg) {
         import std.stdio;

         // Problem code
         if (*this.state == state.ONE)
             "ONE".writeln;
     }
}

void main() {
     State program_state = State.TWO;
     auto a = TestA(1);
     a.method(program_state);
}


More information about the Digitalmars-d mailing list