const Propagation

Daniel Kozak via Digitalmars-d digitalmars-d at puremagic.com
Mon Dec 29 06:13:19 PST 2014


On Monday, 29 December 2014 at 13:22:41 UTC, Julian Kranz wrote:
> So I hope you understand; I've got no problem with changing the 
> type of the function g as you supposed. The bad thing is the 
> additional overload that results in duplicated code.

So you can write something like this:

import std.stdio;

class Hugo {
      public int x = 42;

      void blah(this T)(void function(T h) f)
      {
          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;
      void function(const Hugo h) g = function(const Hugo h) {
          writeln("foobar");
      };
      inge.blah(g);
}


More information about the Digitalmars-d mailing list