digitalmars.D.learn - Templates in classes => what is wrong?
- Xan (27/27) Apr 15 2012 hi,
- John Chapman (7/14) Apr 15 2012 Should be:
- Xan (11/26) Apr 15 2012 It does not work:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (32/60) Apr 15 2012 Your code is still missing 'new':
- jerro (1/3) Apr 15 2012 Why not just declare main return type to be void?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (3/6) Apr 15 2012 That's much better. :) D takes care of doing the right thing in that cas...
- Xan (22/89) Apr 16 2012 With only this change, I receive this error:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/106) Apr 16 2012 Sorry to hear that it is still broken. I've only now realized that you
- Kenji Hara (10/111) Apr 16 2012 2.057 and earlier (You may use gdc 2.057 and command line wrapper
- Xan (6/129) Apr 17 2012 Thanks, Kenji. If I change function to delegate in declaration of
- Xan (9/9) Apr 17 2012 Off-topic, could can I define toString having this structure:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/13) Apr 17 2012 std.string.format is easy:
- Xan (14/34) Apr 17 2012 How to get the "code" of a function or delegate
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (32/45) Apr 17 2012 I don't think D has any help there. You can keep the function as a
- Xan (50/105) Apr 17 2012 Domain is the set of values that we pass to the function and
- Xan (6/53) Apr 17 2012 Solved with typeid:
- Xan (11/131) Apr 17 2012 The idea is behind this https://gist.github.com/2407923
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (19/24) Apr 17 2012 (codi)
- Dejan Lekic (4/140) Apr 17 2012 For an example, you can't use function-pointer to access
- Xan (2/144) Apr 17 2012 So, I deduce it's better to use delegater than function?
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.
Apr 15 2012
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;}
Apr 15 2012
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: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.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;}
Apr 15 2012
On 04/15/2012 11:39 AM, Xan wrote:On Sunday, 15 April 2012 at 11:23:37 UTC, John Chapman wrote:valueOn Sunday, 15 April 2012 at 11:16:43 UTC, Xan wrote: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 noint 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;}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
Apr 15 2012
- 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?
Apr 15 2012
On 04/15/2012 01:27 PM, jerro wrote:That's much better. :) D takes care of doing the right thing in that case. Ali- 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?
Apr 15 2012
On Sunday, 15 April 2012 at 19:30:27 UTC, Ali Çehreli wrote:On 04/15/2012 11:39 AM, Xan wrote: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)On Sunday, 15 April 2012 at 11:23:37 UTC, John Chapman wrote:no valueOn Sunday, 15 April 2012 at 11:16:43 UTC, Xan wrote:It does not work: $ gdmd-4.6 algorisme.d algorisme.d:18: Error: variable algorisme.main.alg voids haveint 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;}algorisme.d:18: Error: expression class Algorisme is void andhas no valuewith 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; } AliWith 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"?) ;-)
Apr 16 2012
On 04/16/2012 11:48 AM, Xan wrote:On Sunday, 15 April 2012 at 19:30:27 UTC, Ali Çehreli wrote: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)On 04/15/2012 11:39 AM, Xan wrote: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)On Sunday, 15 April 2012 at 11:23:37 UTC, John Chapman wrote:no valueOn Sunday, 15 April 2012 at 11:16:43 UTC, Xan wrote:It does not work: $ gdmd-4.6 algorisme.d algorisme.d:18: Error: variable algorisme.main.alg voids haveint 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;}algorisme.d:18: Error: expression class Algorisme is void andhas no valuewith 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; } AliWith 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"?) ;-)Google Translate helped me there. :) Ali
Apr 16 2012
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:Problem may be here:On 04/15/2012 11:39 AM, Xan wrote: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)On Sunday, 15 April 2012 at 11:23:37 UTC, John Chapman wrote:no valueOn Sunday, 15 April 2012 at 11:16:43 UTC, Xan wrote:It does not work: $ gdmd-4.6 algorisme.d algorisme.d:18: Error: variable algorisme.main.alg voids haveint 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;}algorisme.d:18: Error: expression class Algorisme is void andhas no valuewith 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; } AliWith 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"?) ;-)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
Apr 16 2012
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: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.On Sunday, 15 April 2012 at 19:30:27 UTC, Ali Çehreli wrote:Problem may be here:On 04/15/2012 11:39 AM, Xan wrote: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)On Sunday, 15 April 2012 at 11:23:37 UTC, John Chapman wrote:no valueOn Sunday, 15 April 2012 at 11:16:43 UTC, Xan wrote:It does not work: $ gdmd-4.6 algorisme.d algorisme.d:18: Error: variable algorisme.main.alg voids haveint 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;}algorisme.d:18: Error: expression class Algorisme is void andhas no valuewith 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; } AliWith 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"?) ;-)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
Apr 17 2012
Off-topic, could can I define toString having this structure: <Name of the function> (versió <version number>): <Domain> -> <Range>, <code of the function> ? (For example, in https://gist.github.com/2394274 I want that Doblar displays as: Doblar (versió 1): int -> int, { return 2 * a; } Thanks a lot, Xan.
Apr 17 2012
On 04/17/2012 08:17 AM, Xan wrote:Off-topic, could can I define toString having this structure: <Name of the function> (versió <version number>): <Domain> -> <Range>, <code of the function> ? (For example, in https://gist.github.com/2394274 I want that Doblar displays as: Doblar (versió 1): int -> int, { return 2 * a; } Thanks a lot, Xan.std.string.format is easy: format("%s%s", 42, "hello"); Ali
Apr 17 2012
On Tuesday, 17 April 2012 at 15:30:36 UTC, Ali Çehreli wrote:On 04/17/2012 08:17 AM, Xan wrote:How to get the "code" of a function or delegate |___string toString() { |___|___return format("%s (versió %s): Domini -> Recorregut, %s(x) = %s", nom, versio, nom, &funcio); |___} does not produce the desired result and &funcio without ampersand produces me an error. So, my doubts are: given a function: - how can I get the domain - how can I get the range - how can I get the code of the function? See https://gist.github.com/2394274Off-topic, could can I define toString having this structure: <Name of the function> (versió <version number>): <Domain> -> <Range>, <code of the function> ? (For example, in https://gist.github.com/2394274 I want that Doblar displays as: Doblar (versió 1): int -> int, { return 2 * a; } Thanks a lot, Xan.std.string.format is easy: format("%s%s", 42, "hello"); Ali
Apr 17 2012
On 04/17/2012 08:42 AM, Xan wrote:How to get the "code" of a function or delegate |___string toString() { |___|___return format("%s (versió %s): Domini -> Recorregut, %s(x) = %s", nom, versio, nom, &funcio); |___} does not produce the desired result and &funcio without ampersand produces me an error. So, my doubts are: given a function: - how can I get the domain - how can I get the rangeI did not understand those. :(- how can I get the code of the function? See https://gist.github.com/2394274I don't think D has any help there. You can keep the function as a string yourself and convert to actual code at compile time with a string mixin. For that to happen, the function text may be an additional template parameter: import std.conv, std.stdio, std.stream, std.string; import std.socket, std.socketstream; import std.datetime; class Algorisme(U,V,string funcioText) { string nom; uint versio; alias V delegate (U) Funcio; Funcio funcio; this(string nom, uint versio) { this.nom = nom; this.versio = versio; this.funcio = mixin(funcioText); } string toString() { return format("%s (versió %s): Domini -> Recorregut, %s(x) = %s", nom, versio, nom, funcioText); } } alias Algorisme!(int, int, "(int a) { return 2 * a; }") AlgorismeEnters; void main(string [] args) { auto alg = new AlgorismeEnters("Doblar", 1); writeln(alg); } Ali
Apr 17 2012
On Tuesday, 17 April 2012 at 15:59:25 UTC, Ali Çehreli wrote:On 04/17/2012 08:42 AM, Xan wrote:Domain is the set of values that we pass to the function and Range is the set of Values which are returned by function. V delegate (U) f; f has Domain U and Range V I want to "print" the type of "U" and "V". Something like: class Algorisme(U,V) { string nom; uint versio; alias V delegate (U) Funcio; Funcio funcio; this(string nom, uint versio, Funcio funcio) { this.nom = nom; this.versio = versio; this.funcio = funcio; } string toString() { return format("%s (versió %s): %s -> %s, %s(x) = %s", nom, versio, V, U, nom, &funcio); } } but I receive algorisme.d:24: Error: type int has no value algorisme.d:24: Error: type int has no value when I call with Algorisme!(int, int) intead of receiving "int" and "int" as Domain and RangeHow to get the "code" of a function or delegate |___string toString() { |___|___return format("%s (versió %s): Domini -> Recorregut,%s(x) =%s", nom, versio, nom, &funcio); |___} does not produce the desired result and &funcio withoutampersandproduces me an error. So, my doubts are: given a function: - how can I get the domain - how can I get the rangeI did not understand those. :(It's ugly code. I think I could call some procedure like f.code (f is a function) to obtain the "code" how f is defined. But stricly in mathematical thinking it's not adequate, because more codes could result in the same (mathematical function): x + x is the double of x; and 2*x is too. Perhaps if I change Algorisme and add the string field "code" and if there is any procedure to copy the 3rd argument in the constructor and pass as string in the 4th argument (in the constructor) class Algorisme(U,V) { |___string nom; |___uint versio; |___alias V delegate (U) Funcio; |___Funcio funcio; |___string code; |___this(string nom, uint versio, Funcio funcio) { |___|___this.nom = nom; |___|___this.versio = versio; |___|___this.funcio = funcio; this.code = funcio.WHAT PROCEDURE?; |___} Regards, Xan.- how can I get the code of the function? See https://gist.github.com/2394274I don't think D has any help there. You can keep the function as a string yourself and convert to actual code at compile time with a string mixin. For that to happen, the function text may be an additional template parameter: import std.conv, std.stdio, std.stream, std.string; import std.socket, std.socketstream; import std.datetime; class Algorisme(U,V,string funcioText) { string nom; uint versio; alias V delegate (U) Funcio; Funcio funcio; this(string nom, uint versio) { this.nom = nom; this.versio = versio; this.funcio = mixin(funcioText); } string toString() { return format("%s (versió %s): Domini -> Recorregut, %s(x) = %s", nom, versio, nom, funcioText); } } alias Algorisme!(int, int, "(int a) { return 2 * a; }") AlgorismeEnters; void main(string [] args) { auto alg = new AlgorismeEnters("Doblar", 1); writeln(alg); } Ali
Apr 17 2012
On Tuesday, 17 April 2012 at 18:00:55 UTC, Xan wrote:On Tuesday, 17 April 2012 at 15:59:25 UTC, Ali Çehreli wrote:Solved with typeid: string toString() { |___|___return format("%s (versió %s): %s -> %s, %s(x) = %s", nom, versio, typeid(V), typeid(U), nom, &funcio); |___}On 04/17/2012 08:42 AM, Xan wrote:Domain is the set of values that we pass to the function and Range is the set of Values which are returned by function. V delegate (U) f; f has Domain U and Range V I want to "print" the type of "U" and "V". Something like: class Algorisme(U,V) { string nom; uint versio; alias V delegate (U) Funcio; Funcio funcio; this(string nom, uint versio, Funcio funcio) { this.nom = nom; this.versio = versio; this.funcio = funcio; } string toString() { return format("%s (versió %s): %s -> %s, %s(x) = %s", nom, versio, V, U, nom, &funcio); } } but I receive algorisme.d:24: Error: type int has no value algorisme.d:24: Error: type int has no valueHow to get the "code" of a function or delegate |___string toString() { |___|___return format("%s (versió %s): Domini -> Recorregut,%s(x) =%s", nom, versio, nom, &funcio); |___} does not produce the desired result and &funcio withoutampersandproduces me an error. So, my doubts are: given a function: - how can I get the domain - how can I get the rangeI did not understand those. :(
Apr 17 2012
On Tuesday, 17 April 2012 at 18:00:55 UTC, Xan wrote:On Tuesday, 17 April 2012 at 15:59:25 UTC, Ali Çehreli wrote:The idea is behind this https://gist.github.com/2407923 But I receive: $ gdmd-4.6 algorisme_code.d algorisme_code.d:22: Error: variable codi cannot be read at compile time algorisme_code.d:22: Error: argument to mixin must be a string, not (codi) What can I do? Thanks, Xan.On 04/17/2012 08:42 AM, Xan wrote:Domain is the set of values that we pass to the function and Range is the set of Values which are returned by function. V delegate (U) f; f has Domain U and Range V I want to "print" the type of "U" and "V". Something like: class Algorisme(U,V) { string nom; uint versio; alias V delegate (U) Funcio; Funcio funcio; this(string nom, uint versio, Funcio funcio) { this.nom = nom; this.versio = versio; this.funcio = funcio; } string toString() { return format("%s (versió %s): %s -> %s, %s(x) = %s", nom, versio, V, U, nom, &funcio); } } but I receive algorisme.d:24: Error: type int has no value algorisme.d:24: Error: type int has no value when I call with Algorisme!(int, int) intead of receiving "int" and "int" as Domain and RangeHow to get the "code" of a function or delegate |___string toString() { |___|___return format("%s (versió %s): Domini -> Recorregut,%s(x) =%s", nom, versio, nom, &funcio); |___} does not produce the desired result and &funcio withoutampersandproduces me an error. So, my doubts are: given a function: - how can I get the domain - how can I get the rangeI did not understand those. :(It's ugly code. I think I could call some procedure like f.code (f is a function) to obtain the "code" how f is defined. But stricly in mathematical thinking it's not adequate, because more codes could result in the same (mathematical function): x + x is the double of x; and 2*x is too. Perhaps if I change Algorisme and add the string field "code" and if there is any procedure to copy the 3rd argument in the constructor and pass as string in the 4th argument (in the constructor) class Algorisme(U,V) { |___string nom; |___uint versio; |___alias V delegate (U) Funcio; |___Funcio funcio; |___string code; |___this(string nom, uint versio, Funcio funcio) { |___|___this.nom = nom; |___|___this.versio = versio; |___|___this.funcio = funcio; this.code = funcio.WHAT PROCEDURE?; |___} Regards, Xan.- how can I get the code of the function? See https://gist.github.com/2394274I don't think D has any help there. You can keep the function as a string yourself and convert to actual code at compile time with a string mixin. For that to happen, the function text may be an additional template parameter: import std.conv, std.stdio, std.stream, std.string; import std.socket, std.socketstream; import std.datetime; class Algorisme(U,V,string funcioText) { string nom; uint versio; alias V delegate (U) Funcio; Funcio funcio; this(string nom, uint versio) { this.nom = nom; this.versio = versio; this.funcio = mixin(funcioText); } string toString() { return format("%s (versió %s): Domini -> Recorregut, %s(x) = %s", nom, versio, nom, funcioText); } } alias Algorisme!(int, int, "(int a) { return 2 * a; }") AlgorismeEnters; void main(string [] args) { auto alg = new AlgorismeEnters("Doblar", 1); writeln(alg); } Ali
Apr 17 2012
On 04/17/2012 11:13 AM, Xan wrote:The idea is behind this https://gist.github.com/2407923 But I receive: $ gdmd-4.6 algorisme_code.d algorisme_code.d:22: Error: variable codi cannot be read at compile time algorisme_code.d:22: Error: argument to mixin must be a string, not(codi) mixin is about code generation. For that reason the string that is given to it must be available at compile time. Upon analyzing the code, that is the case in your example, but because mixin() appears inside the constructor, it cannot use a string parameter. That's why I had used a template parameter for the function string. There may be a number of solutions but only you can decide on what to do. One solution is to mixin the delegate outside of the constructor and pass as an argument along with its string representation: // Untested code this(... Funcio funcio, string funcioText) { ... } In main: enum funcioText = "..."; auto funcio = mixin(funcioText); ... new Algorisme(..., funcio, funcioText); Ali
Apr 17 2012
On Tuesday, 17 April 2012 at 18:25:21 UTC, Ali Çehreli wrote:On 04/17/2012 11:13 AM, Xan wrote:What is change is this code? Is it the same as this https://gist.github.com/2407923 (I revise the code)? With my (v. 2) code I receive the errors: $ gdmd-4.6 algorisme_code.d algorisme_code.d:44: Error: variable codi cannot be read at compile time algorisme_code.d:44: Error: argument to mixin must be a string, not (codi) algorisme_code.d:45: Error: constructor algorisme_code.Algorisme!(int,int).Algorisme.this (string nom, uint versio, int function(int) funcio, string codi) is not callable using argument types (string,int,_error_,string) Why the string is not given at compile code? I don't understand it! Xan.The idea is behind this https://gist.github.com/2407923 But I receive: $ gdmd-4.6 algorisme_code.d algorisme_code.d:22: Error: variable codi cannot be read atcompile timealgorisme_code.d:22: Error: argument to mixin must be astring, not (codi) mixin is about code generation. For that reason the string that is given to it must be available at compile time. Upon analyzing the code, that is the case in your example, but because mixin() appears inside the constructor, it cannot use a string parameter. That's why I had used a template parameter for the function string. There may be a number of solutions but only you can decide on what to do. One solution is to mixin the delegate outside of the constructor and pass as an argument along with its string representation: // Untested code this(... Funcio funcio, string funcioText) { ... } In main: enum funcioText = "..."; auto funcio = mixin(funcioText); ... new Algorisme(..., funcio, funcioText); Ali
Apr 17 2012
On Tuesday, 17 April 2012 at 18:39:16 UTC, Xan wrote:On Tuesday, 17 April 2012 at 18:25:21 UTC, Ali Çehreli wrote:It works with enum instead of string: https://gist.github.com/2407923 Thanks all of you, Xan.On 04/17/2012 11:13 AM, Xan wrote:What is change is this code? Is it the same as this https://gist.github.com/2407923 (I revise the code)? With my (v. 2) code I receive the errors: $ gdmd-4.6 algorisme_code.d algorisme_code.d:44: Error: variable codi cannot be read at compile time algorisme_code.d:44: Error: argument to mixin must be a string, not (codi) algorisme_code.d:45: Error: constructor algorisme_code.Algorisme!(int,int).Algorisme.this (string nom, uint versio, int function(int) funcio, string codi) is not callable using argument types (string,int,_error_,string) Why the string is not given at compile code? I don't understand it! Xan.The idea is behind this https://gist.github.com/2407923 But I receive: $ gdmd-4.6 algorisme_code.d algorisme_code.d:22: Error: variable codi cannot be read atcompile timealgorisme_code.d:22: Error: argument to mixin must be astring, not (codi) mixin is about code generation. For that reason the string that is given to it must be available at compile time. Upon analyzing the code, that is the case in your example, but because mixin() appears inside the constructor, it cannot use a string parameter. That's why I had used a template parameter for the function string. There may be a number of solutions but only you can decide on what to do. One solution is to mixin the delegate outside of the constructor and pass as an argument along with its string representation: // Untested code this(... Funcio funcio, string funcioText) { ... } In main: enum funcioText = "..."; auto funcio = mixin(funcioText); ... new Algorisme(..., funcio, funcioText); Ali
Apr 18 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: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).On Monday, 16 April 2012 at 18:48:52 UTC, Xan wrote: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.On Sunday, 15 April 2012 at 19:30:27 UTC, Ali Çehreli wrote:Problem may be here:On 04/15/2012 11:39 AM, Xan wrote: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)On Sunday, 15 April 2012 at 11:23:37 UTC, John Chapman wrote:no valueOn Sunday, 15 April 2012 at 11:16:43 UTC, Xan wrote:It does not work: $ gdmd-4.6 algorisme.d algorisme.d:18: Error: variable algorisme.main.alg voids haveint 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;}algorisme.d:18: Error: expression class Algorisme is void andhas no valuewith 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; } AliWith 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"?) ;-)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
Apr 17 2012
On Tuesday, 17 April 2012 at 15:21:30 UTC, Dejan Lekic wrote:On Tuesday, 17 April 2012 at 14:57:18 UTC, Xan wrote:So, I deduce it's better to use delegater than function?On Tuesday, 17 April 2012 at 01:31:43 UTC, Kenji Hara wrote: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).On Monday, 16 April 2012 at 18:48:52 UTC, Xan wrote: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.On Sunday, 15 April 2012 at 19:30:27 UTC, Ali Çehreli wrote:Problem may be here:On 04/15/2012 11:39 AM, Xan wrote: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)On Sunday, 15 April 2012 at 11:23:37 UTC, John Chapman wrote:no valueOn Sunday, 15 April 2012 at 11:16:43 UTC, Xan wrote:It does not work: $ gdmd-4.6 algorisme.d algorisme.d:18: Error: variable algorisme.main.alg voids haveint 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;}algorisme.d:18: Error: expression class Algorisme is void andhas no valuewith 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; } AliWith 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"?) ;-)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
Apr 17 2012