Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
August 11, 2014 Destroy two assumptions: interface implementation generated by TMP | ||||
---|---|---|---|---|
| ||||
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'. |
August 12, 2014 Re: Destroy two assumptions: interface implementation generated by TMP | ||||
---|---|---|---|---|
| ||||
Posted in reply to Baz | 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.
|
August 12, 2014 Re: Destroy two assumptions: interface implementation generated by TMP | ||||
---|---|---|---|---|
| ||||
Posted in reply to Baz | 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. |
August 12, 2014 Re: Destroy two assumptions: interface implementation generated by TMP | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | 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?
|
August 12, 2014 Re: Destroy two assumptions: interface implementation generated by TMP | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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.
|
August 12, 2014 Re: Destroy two assumptions: interface implementation generated by TMP | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | 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.
|
Copyright © 1999-2021 by the D Language Foundation