Templates in classes => what is wrong?
Xan
xancorreu at gmail.com
Mon Apr 16 11:48:51 PDT 2012
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"?) ;-)
More information about the Digitalmars-d-learn
mailing list