Templates in classes => what is wrong?

Dejan Lekic dejan.lekic at gmail.com
Tue Apr 17 08:21:29 PDT 2012


On Tuesday, 17 April 2012 at 14:57:18 UTC, Xan wrote:
> On Tuesday, 17 April 2012 at 01:31:43 UTC, Kenji Hara wrote:
>> On Monday, 16 April 2012 at 18:48:52 UTC, Xan wrote:
>>> On Sunday, 15 April 2012 at 19:30:27 UTC, Ali Çehreli wrote:
>>>> On 04/15/2012 11:39 AM, Xan wrote:
>>>> > On Sunday, 15 April 2012 at 11:23:37 UTC, John Chapman 
>>>> > wrote:
>>>> >> On Sunday, 15 April 2012 at 11:16:43 UTC, Xan wrote:
>>>> >>>
>>>> >>> int main(string [] args)
>>>> >>> {
>>>> >>> auto alg = Algorisme!(int,int);
>>>> >>
>>>> >> Should be:
>>>> >> auto alg = new Algorisme!(int, int);
>>>> >>
>>>> >>> alg.nom = "Doblar";
>>>> >>> alg.versio = 1;
>>>> >>> alg.funcio = (int a) {return 2*a};
>>>> >>
>>>> >> Should be:
>>>> >> alg.funcio = (int a) { return 2 * a; };
>>>> >> or:
>>>> >> alg.funcio = a => 2 * a;
>>>> >>
>>>> >>> }
>>>> >
>>>> >
>>>> > It does not work:
>>>> >
>>>> > $ gdmd-4.6 algorisme.d
>>>> > algorisme.d:18: Error: variable algorisme.main.alg voids 
>>>> > have
>>>> no value
>>>> > algorisme.d:18: Error: expression class Algorisme is void 
>>>> > and
>>>> has no value
>>>> >
>>>> > with the code https://gist.github.com/2394274
>>>> >
>>>> > What fails now?
>>>> >
>>>> > Thanks,
>>>> > Xan.
>>>>
>>>> Your code is still missing 'new':
>>>>
>>>> 	auto alg = new Algorisme!(int, int);
>>>
>>> With only this change, I receive this error:
>>>
>>> $ gdmd-4.6 algorisme.d
>>> algorisme.d:21: Error: cannot implicitly convert expression 
>>> (__dgliteral1) of type int delegate(int a) pure nothrow to 
>>> int function(int)
>>>
>>>>
>>>> Unrelated recommendations:
>>>>
>>>> - Return 0 from main() for successful exit, anything else by 
>>>> convention means some sort of error.
>>>>
>>>> - Take advantage of constructors (and 'alias') to simplify 
>>>> syntax and risk of bugs:
>>>>
>>>> import std.conv, std.stdio, std.stream, std.string;
>>>> import std.socket, std.socketstream;
>>>> import std.datetime;
>>>>
>>>> class Algorisme(U,V) {
>>>>  string nom;
>>>>  uint versio;
>>>>  alias V function (U) Funcio;
>>>>  Funcio funcio;
>>>>
>>>>  this(string nom, uint versio, Funcio funcio)
>>>>  {
>>>>      this.nom = nom;
>>>>      this.versio = versio;
>>>>      this.funcio = funcio;
>>>>  }
>>>> }
>>>>
>>>> int main(string [] args)
>>>> {
>>>>  alias Algorisme!(int, int) MeuAlgorism;
>>>>  auto alg = new MeuAlgorism("Doblar", 1,
>>>>                             (int a) { return 2 * a; });
>>>>
>>>>  return 0;
>>>> }
>>>>
>>>> Ali
>>>
>>> With all of your suggestion 
>>> [https://gist.github.com/2394274], I get:
>>>
>>> $ gdmd-4.6 algorisme.d
>>> algorisme.d:30: Error: constructor 
>>> algorisme.Algorisme!(int,int).Algorisme.this (string nom, 
>>> uint versio, int function(int) funcio) is not callable using 
>>> argument types (string,int,int delegate(int a) pure nothrow)
>>> algorisme.d:30: Error: cannot implicitly convert expression 
>>> (__dgliteral1) of type int delegate(int a) pure nothrow to 
>>> int function(int)
>>> algorisme.d:27: Error: function D main has no return 
>>> statement, but is expected to return a value of type int
>>>
>>>
>>> What fails?
>>>
>>> PS: Thanks for your recommendations...
>>> PPS: By the other hand, I see you have learned catalan 
>>> ("MeuAlgorisme"?) ;-)
>>
>> Problem may be here:
>>
>>> alg.funcio = (int a) { return 2 * a; };
>>
>> 2.057 and earlier (You may use gdc 2.057 and command line 
>> wrapper gdmd), function literal always deduced as 'delegate'. 
>> So this expression raises an error about type mismatching Lhs 
>> of 'int function(int)' and  Rhs of 'int delegate(int) pure 
>> nothrow'.
>>
>> Then, specifying explicit 'function' will resolve issue:
>>
>>  alg.funcio = function(int a) { return 2 * a; };
>>
>> Bye.
>>
>> Kenji Hara
>
> Thanks, Kenji. If I change function to delegate in declaration 
> of field, it works too. What do you recommend to have delegates 
> or functions? What are the benefits and ...
>
> Thanks,
> Xan.

For an example, you can't use function-pointer to access 
non-static methods, while with delegates you can. You can see 
some examples on http://www.dlang.org (Languate Reference).


More information about the Digitalmars-d-learn mailing list