What is the correct way to forward method calls to the struct field?

monnoroch via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 21 11:11:17 PDT 2014


I tried this:

struct S {
	R opDispatch(string name, R, Args...)(Args args) {
		return mixin("(*iface)." ~ name)(iface, args);
	}
	SomeT ** iface;
}

But then val.foo(1, 2) gives me "no property 'foo' for type 'S'".

I've seen the template solution:

struct S {
	template opDispatch(string name) {
		R opDispatch(R, Args...)(Args args) {
			return mixin("(*iface)." ~ name)(iface, args);
		}
	}
	SomeT ** iface;
}

But that does not seem to work anymore.

I've tried then

struct S {
	auto ifc() {
		return *iface;
	}

	auto ifc() const {
		return *iface;
	}
	
	SomeT ** iface;
}

And that worked, but it's not perfect: i also want to pass iface
as a first argument.

val.foo(1, 2) -> (*val.iface).foo(val.iface, 1, 2).

Any suggestions?


More information about the Digitalmars-d-learn mailing list