View mode: basic / threaded / horizontal-split · Log in · Help
April 15, 2012
Templates in classes => what is wrong?
hi,

I have this code:

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;
    V delegate (U) funcio;

}

int main(string [] args)
{
    auto alg = Algorisme!(int,int);
    alg.nom = "Doblar";
    alg.versio = 1;
    alg.funcio = (int a) {return 2*a};
}

but when I compile I receive errors:

$ gdmd-4.6 algorisme.d
algorisme.d:22: found '}' when expecting ';' following return 
statement
algorisme.d:25: found 'EOF' when expecting ';' following statement
algorisme.d:25: found 'EOF' when expecting '}' following compound 
statement


What is wrong?

Thanks in advance,
Xan.
April 15, 2012
Re: Templates in classes => what is wrong?
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;

> }
April 15, 2012
Re: Templates in classes => what is wrong?
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.
April 15, 2012
Re: Templates in classes => what is wrong?
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
April 15, 2012
Re: Templates in classes => what is wrong?
> - Return 0 from main() for successful exit, anything else by 
> convention means some sort of error.

Why not just declare main return type to be void?
April 15, 2012
Re: Templates in classes => what is wrong?
On 04/15/2012 01:27 PM, jerro wrote:
>> - Return 0 from main() for successful exit, anything else by
>> convention means some sort of error.
>
> Why not just declare main return type to be void?

That's much better. :) D takes care of doing the right thing in that case.

Ali
April 16, 2012
Re: Templates in classes => what is wrong?
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"?) ;-)
April 16, 2012
Re: Templates in classes => what is wrong?
On 04/16/2012 11:48 AM, 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?

Sorry to hear that it is still broken. I've only now realized that you 
are using gdmd. Your code works with dmd 2.059 with just one fix:

Make the return type of main() void:

void main(string [] args)

>
> PS: Thanks for your recommendations...
> PPS: By the other hand, I see you have learned catalan ("MeuAlgorisme"?)
> ;-)

Google Translate helped me there. :)

Ali
April 17, 2012
Re: Templates in classes => what is wrong?
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
April 17, 2012
Re: Templates in classes => what is wrong?
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.
« First   ‹ Prev
1 2 3
Top | Discussion index | About this forum | D home