www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Destroy two assumptions: interface implementation generated by TMP

reply "Baz" <basile.burg gmx.com> writes:
Hi, I try to get why the last way of generating an interface 
implementation fails. I've put assumptions: is it right ?
-------------------------------
module itfgen;

import std.stdio;

interface itf{
     void a_int(int p);
     void a_uint(uint p);
}

template genimpl(T){
     char[] genimpl(){
         char[] result;
         result = "void a_" ~ T.stringof ~ "("~ T.stringof ~ " 
p){}".dup;
         return result;
     }
}

class impl: itf{
     mixin(genimpl!int);
     mixin(genimpl!uint);
}

// OK because: mixin is at the source code level, begining of the 
ana
class impl2: itf{
     static char[] genimplint(T)(){
         char[] result;
         result = "void a_" ~ T.stringof ~ "("~ T.stringof ~ " 
p){}".dup;
         return result;
     }
     mixin(genimplint!int);
     mixin(genimplint!uint);
}

// FAILS because: alias are probably generated after the itf check
class impl3: itf{
     void tmp(T)(T p){};
     alias a_int = tmp!int;
     alias a_uint = tmp!uint;
}

void main(string args[]){
     auto I1 = new impl;
     auto I2 = new impl2;
     auto I3 = new impl3;
}
-------------------------------

I get

'Error: class itfgen.impl3 interface function 'void a_int(int p)' 
is not implemented'.
Aug 11 2014
next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Monday, 11 August 2014 at 18:21:04 UTC, Baz wrote:
 Hi, I try to get why the last way of generating an interface 
 implementation fails. I've put assumptions: is it right ?
 -------------------------------
 module itfgen;

 import std.stdio;

 interface itf{
     void a_int(int p);
     void a_uint(uint p);
 }

 class impl3: itf{
     void tmp(T)(T p){};
     alias a_int = tmp!int;
     alias a_uint = tmp!uint;
 }
Part of me feels that ought to work. There may well be good reasons why not though. Currently you do have to have method implementations/overrides written out as normal functions.
Aug 12 2014
prev sibling parent reply "anonymous" <anonymous example.com> writes:
On Monday, 11 August 2014 at 18:21:04 UTC, Baz wrote:
 interface itf{
     void a_int(int p);
     void a_uint(uint p);
 }
[...]
 // FAILS because: alias are probably generated after the itf 
 check
 class impl3: itf{
     void tmp(T)(T p){};
     alias a_int = tmp!int;
     alias a_uint = tmp!uint;
 }
[...]
 'Error: class itfgen.impl3 interface function 'void a_int(int 
 p)' is not implemented'.
I think the problem is that impl3.tmp is not virtual because it's a template, and interfaces need to be implemented by virtual methods.
Aug 12 2014
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 12 August 2014 at 11:34:01 UTC, anonymous wrote:
 On Monday, 11 August 2014 at 18:21:04 UTC, Baz wrote:
 interface itf{
    void a_int(int p);
    void a_uint(uint p);
 }
[...]
 // FAILS because: alias are probably generated after the itf 
 check
 class impl3: itf{
    void tmp(T)(T p){};
    alias a_int = tmp!int;
    alias a_uint = tmp!uint;
 }
[...]
 'Error: class itfgen.impl3 interface function 'void a_int(int 
 p)' is not implemented'.
I think the problem is that impl3.tmp is not virtual because it's a template, and interfaces need to be implemented by virtual methods.
The instantiations of the template are just normal functions though, no?
Aug 12 2014
parent reply "anonymous" <anonymous example.com> writes:
On Tuesday, 12 August 2014 at 12:08:14 UTC, John Colvin wrote:
 I think the problem is that impl3.tmp is not virtual because 
 it's
 a template, and interfaces need to be implemented by virtual
 methods.
The instantiations of the template are just normal functions though, no?
They are not virtual. Ordinary methods (public, not final, not templated) are virtual.
Aug 12 2014
parent "Baz" <basile.burg gmx.com> writes:
On Tuesday, 12 August 2014 at 12:43:46 UTC, anonymous wrote:
 On Tuesday, 12 August 2014 at 12:08:14 UTC, John Colvin wrote:
 I think the problem is that impl3.tmp is not virtual because 
 it's
 a template, and interfaces need to be implemented by virtual
 methods.
The instantiations of the template are just normal functions though, no?
They are not virtual. Ordinary methods (public, not final, not templated) are virtual.
The virtual stuff is right. I've just realized that it's written black on white at the bottom of the manual page which describes templates: "Templates cannot be used to add non-static members or virtual functions to classes" "Templates cannot add functions to interfaces" Sorry for the loss of time.
Aug 12 2014