Thread overview
Pointer to interface method
Apr 15, 2007
Mandel
Apr 15, 2007
Frits van Bommel
Apr 15, 2007
BCS
Apr 16, 2007
Mandel
April 15, 2007
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?

April 15, 2007
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.
April 15, 2007
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.


April 16, 2007
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.
I want to call methods on objects that implement a specific interface.
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)
Maybe there is something similar in D to achieve the same concept?