Templates in classes => what is wrong?

Ali Çehreli acehreli at yahoo.com
Sun Apr 15 12:30:26 PDT 2012


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);

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



More information about the Digitalmars-d-learn mailing list