inout, delegates, and visitor functions.
ponce via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Oct 24 04:11:31 PDT 2015
On Saturday, 24 October 2015 at 08:51:58 UTC, Sebastien Alaiwan
wrote:
> 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!
Hi Sebastien,
That was an interesting question and I didn't succeed with
'inout' either without duplicating apply.
I have a partial solution here:
http://dpaste.dzfl.pl/b5ec7f16b912 which templatizes the delegate
type, but is probably not what you want.
The qualifier is not carried on to the apply() function. When
taking a const delegate it will still not be const.
More information about the Digitalmars-d-learn
mailing list