www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pointer to interface method

reply Mandel <mandel mailinator.com> writes:
Hi,

I like to have a pointer to an interface method,
like this:

interface Item
{
	void work();
}

int main(char[][] args)
{
	
	auto s = &Item.work
	return 0;
}

But this example fails because of an "undefined symbol" for Item.work.
Is this a bug or intended behaviour?
Apr 15 2007
next sibling parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Mandel wrote:
 Hi,
 
 I like to have a pointer to an interface method,

What possible use could this have? Interface methods are by definition abstract, and are thus never directly defined. (Note that D doesn't support the concept of a "member pointer" as C++ does)
 like this:
 
 interface Item
 {
 	void work();
 }
 
 int main(char[][] args)
 {
 	
 	auto s = &Item.work
 	return 0;
 }
 
 But this example fails because of an "undefined symbol" for Item.work.

Actually, it fails with "semicolon expected following auto declaration, not 'return'" :).
 Is this a bug or intended behaviour?

What happens here is that the compiler tells the linker to fill in a pointer to the relevant method at link time, but in the case of an abstract method the symbol simply doesn't exist. There's nothing sensible to do here other than produce an error. The fact that the error isn't produced until link-time instead of at compile-time could be considered a bug though, since the compiler can determine the function can't possibly be defined in a legal program.
Apr 15 2007
prev sibling parent reply BCS <ao pathlink.com> writes:
Reply to Mandel,

 Hi,
 
 I like to have a pointer to an interface method,
 like this:
 interface Item
 {
 void work();
 }
 int main(char[][] args)
 {
 auto s = &Item.work
 return 0;
 }
 But this example fails because of an "undefined symbol" for Item.work.
 Is this a bug or intended behaviour?
 

An interface can't have static methouds so it needs to have an instance to work on: class CItem: Item { void work(){} } Item c = new CItem; auto s = &c.work; somthing like that should work. Note that the delegate you get will depend on the class you use.
Apr 15 2007
parent Mandel <mandel mailinator.com> writes:
Frits van Bommel Wrote:

 Mandel wrote:
 Hi,
 
 I like to have a pointer to an interface method,

What possible use could this have? Interface methods are by definition abstract, and are thus never directly defined.

In normal cases you would simple call the function by it's name and also by interface - trivial. But it would also mean that I have to write code for every interface method class implements and also every interface. In my case I have many interfaces that may also change during development. The behaviour on all functions is determined by their return type (it will be a RPC class). Therefore I would like to create a class that registers interface methods (passed as template argument). This way, all code that handle objects based on a registered interface method will be created at compile time. No need for specific implementations for every interface in this case. :) For example, if I have interfaces A, B, C, D and some classes that implement at least one of these interfaces. I have to write handler code (e.g. a class) for every interface. If I would have a class that allows to store (wrapped) accessors to interface methods, only one class needs to be written - replacing all handler classes for each interface. I hope I was not too confusing. ;-) I have done this in C++. Would be nice if the same could be achieved in D.
 
 (Note that D doesn't support the concept of a "member pointer" as C++ does)

Apr 15 2007