const Propagation

anonymous via Digitalmars-d digitalmars-d at puremagic.com
Mon Dec 29 11:05:35 PST 2014


On Monday, 29 December 2014 at 14:13:20 UTC, Daniel Kozak wrote:
> 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);
> }

So template methods deduce const now? Sweet.

I see one wart on this: `inge.blah((h) {});` doesn't work.


More information about the Digitalmars-d mailing list