I want to implement operation feasible?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 15 20:52:07 PST 2014


On Tuesday, 16 December 2014 at 04:45:31 UTC, Brian wrote:
> interface BaseMod
> {
> 	auto getInstance();
> }

You can't do that - interface methods must be either final or 
have concrete types (they can't be templates or auto returns).

Make it BaseMod getInstance(); and the rest of your classes will 
work.


> 	auto a = modObj::getInstance();

Since this comes from the interface, a will be typed to the 
interface too. It will have the dynamic type of the underlying 
class, but the interface functions will all return the interface.

If your hello method is in the interface, you can just call it at 
that point though and you'll get the results you want.

BTW it is modObj.getInstance rather than ::. D always uses the 
dot.

> 	Object.callMethod(a, "hello");

You could write a reflection method like that in D, but it isn't 
done automatically (partially due to bloat concerns making it 
opt-in). Check out the free sample of my D book:

https://www.packtpub.com/application-development/d-cookbook

The free chapter is about D's reflection. I also go into how to 
make runtime methods like you have in other parts of the book.


More information about the Digitalmars-d-learn mailing list