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);
}

Reply via email to