const Propagation

Peter Alexander via Digitalmars-d digitalmars-d at puremagic.com
Mon Dec 29 05:16:15 PST 2014


You need to overload on const, and also pass in a correctly typed 
function as the argument (you can't call a function with a 
mutable parameter with a const object.


import std.stdio;

class Hugo {
   public int x = 42;

   void blah(void function(Hugo h) f) {
     f(this);
   }

   // OVERLOAD
   void blah(void function(const Hugo h) f) const {
     f(this);
   }
}

void main() {
     Hugo hugo = new Hugo();
     void function(Hugo h) f = function(Hugo h) {
       h.x = 99;
     };
     hugo.blah(f);

     const Hugo inge = hugo;
     // CHANGE TYPE HERE
     void function(const Hugo h) g = function(const Hugo h) {
       writeln("foobar");
     };
     inge.blah(g);
}


More information about the Digitalmars-d mailing list