www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Weird combo of static function, templates, interfaces (that doesn't

reply 0xEAB <desisma heidel.beer> writes:
 void main()
 {
     StaticThingy.register(new SomeAdapter());
     StaticThingy.createSome!Dummy();
 }


 interface FooAdapter
 {
     FooBundle!(Handle) createSome(Handle)();
 }

 private class SomeAdapter : FooAdapter
 {
     Bundle createSome()
     {
         auto w = new Dummy();
         auto c = new FooDummy();
         auto b = Bundle(c, w);
         return b;
     }
 }

 struct StaticThingy
 {
 static:
     FooBundle!(Handle) createSome(Handle)()
     {
         return fooAdapter.createSome!(Handle)();
     }

 // ...
 error: undefined reference to 
 '_D9onlineapp10FooAdapter__T10createSomeTCQBn5DummyZQyMFZSQCd__T9FooBundleTQBiZQp'
 collect2: error: ld returned 1 exit status
 Error: linker exited with status 1
Hey :) During playing around I came up with code similar to the example above. As the title says, this code doesn't link. For the full example please see: https://run.dlang.io/is/mr0u2i - But should this even compile successfully? - Does `SomeAdapter` even implement `FooAdapter` correctly? ~ Elias
Dec 30 2018
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/30/2018 05:05 AM, 0xEAB wrote:

 interface FooAdapter
 {
     FooBundle!(Handle) createSome(Handle)();
 }
Function templates cannot be virtual functions. One reason is the compiler cannot know how large the virtual function table should be, and the other one is it's just a template, not the real thing (i.e. not a function).
 private class SomeAdapter : FooAdapter
 {
     Bundle createSome()
SomeAdapter.createSome is a function unrelated to FooAdapter.createSome. Their only relationship is through "name hiding."
 - Does `SomeAdapter` even implement `FooAdapter` correctly?
Yes because FooAdapter has no virtual function. :p
   ~ Elias
Ali
Dec 30 2018
parent 0xEAB <desisma heidel.beer> writes:
On Sunday, 30 December 2018 at 18:55:10 UTC, Ali Çehreli wrote:
 On 12/30/2018 05:05 AM, 0xEAB wrote:

 interface FooAdapter
 {
     FooBundle!(Handle) createSome(Handle)();
 }
Function templates cannot be virtual functions. One reason is the compiler cannot know how large the virtual function table should be, and the other one is it's just a template, not the real thing (i.e. not a function).
 private class SomeAdapter : FooAdapter
 {
     Bundle createSome()
SomeAdapter.createSome is a function unrelated to FooAdapter.createSome. Their only relationship is through "name hiding."
 - Does `SomeAdapter` even implement `FooAdapter` correctly?
Yes because FooAdapter has no virtual function. :p
   ~ Elias
Ali
Thanks for the technical explanation. ~ Elias
Jan 02