Why I can't call toHash in const context?

Borislav Kosharov bosak at gmail.com
Tue Aug 6 11:14:27 PDT 2013


I have this code:

class Animation {
     string name;
}

struct Key {
     Animation from;
     Animation to;

     this(Animation from, Animation to) {
         this.from = from;
         this.to = to;
     }

     const hash_t opHash() {
         return from.toHash() ^ to.toHash();
     }

     const bool opEquals(ref const Key other) {
         return this.from == other.from && this.to == other.to;
     }
}

One class Animation and another Key. And I get this error:
"opHash.d(15): Error: mutable method object.Object.toHash is not 
callable using a const object"
I need Key to be a struct and I need it to have toHash for 
associative arrays.

I looked in the docs and it said that I need 'const hash_t 
opHash' to do that.

Am I missing something?


More information about the Digitalmars-d mailing list