inout, delegates, and visitor functions.

Sebastien Alaiwan via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Oct 24 01:51:56 PDT 2015


Hi all,

I'm trying to get the following code to work.
(This code is a simplified version of some algebraic type).
Is it possible to only declare one version of the 'apply' 
function?
Or should I declare the const version and the non-const version?

I tried using "inout", but I got the following error:

test.d(28): Error: inout method test.E.apply is not callable 
using a mutable object


class E
{
   void apply(void delegate(inout(E) e) f) inout
   {
     f(this);
   }

   int val;
}

void m()
{
   void setToZero(E e)
   {
     e.val = 0;
   }

   void printValue(const E e)
   {
     import std.stdio;
     writefln("Value: %s", e.val);
   }

   E obj;

   obj.apply(&setToZero);
   obj.apply(&printValue);
}

Thanks!



More information about the Digitalmars-d-learn mailing list