digitalmars.D.learn - Passing function as values and returning functions
- Xan (27/27) Apr 11 2012 Hi,
- Jacob Carlborg (4/28) Apr 11 2012 Use "delegate" or "function" both for the argument type and return type.
- Xan (2/44) Apr 11 2012 How to do that?
- Steven Schveighoffer (8/11) Apr 11 2012 int function(int) g(int function(int a) p) { return p; }
- Xan (22/40) Apr 11 2012 Thanks, Steve, but another problem:
- Steven Schveighoffer (6/10) Apr 11 2012 writeln(g(&f)(1));
- Xan (5/18) Apr 11 2012 Yes, it works, finally.
- James Miller (10/28) Apr 12 2012 Glad you got help Xan, but for future reference can you please keep
- Steven Schveighoffer (3/7) Apr 12 2012 Um... this is d.learn...
- James Miller (5/13) Apr 12 2012 And now I look like an asshole because I wasn't paying attention to the
Hi, Following the thread of Higher-order functions, how can I do to pass a function as a parameter and return a function. That is a something like: import std.functional, std.stdio; int f (int a) { return 2*a; } int delegate (int) g(int function(int a) p) { return p; } void main() { writeln(g(f)(1)); } but it gives me: $ gdmd-4.6 functions.d functions.d:8: Error: cannot implicitly convert expression (p) of type int function(int a) to int delegate(int) functions.d:13: Error: function functions.f (int a) is not callable using argument types () functions.d:13: Error: expected 1 function arguments, not 0 functions.d:13: Error: function functions.g (int function(int a) p) is not callable using argument types (int) functions.d:13: Error: cannot implicitly convert expression (f()) of type int to int function(int a) Thanks, Xan.
Apr 11 2012
On 2012-04-11 13:10, Xan wrote:Hi, Following the thread of Higher-order functions, how can I do to pass a function as a parameter and return a function. That is a something like: import std.functional, std.stdio; int f (int a) { return 2*a; } int delegate (int) g(int function(int a) p) { return p; } void main() { writeln(g(f)(1)); } but it gives me: $ gdmd-4.6 functions.d functions.d:8: Error: cannot implicitly convert expression (p) of type int function(int a) to int delegate(int) functions.d:13: Error: function functions.f (int a) is not callable using argument types () functions.d:13: Error: expected 1 function arguments, not 0 functions.d:13: Error: function functions.g (int function(int a) p) is not callable using argument types (int) functions.d:13: Error: cannot implicitly convert expression (f()) of type int to int function(int a)Use "delegate" or "function" both for the argument type and return type. -- /Jacob Carlborg
Apr 11 2012
On Wednesday, 11 April 2012 at 11:59:14 UTC, Jacob Carlborg wrote:On 2012-04-11 13:10, Xan wrote:How to do that?Hi, Following the thread of Higher-order functions, how can I do to pass a function as a parameter and return a function. That is a something like: import std.functional, std.stdio; int f (int a) { return 2*a; } int delegate (int) g(int function(int a) p) { return p; } void main() { writeln(g(f)(1)); } but it gives me: $ gdmd-4.6 functions.d functions.d:8: Error: cannot implicitly convert expression (p) of type int function(int a) to int delegate(int) functions.d:13: Error: function functions.f (int a) is not callable using argument types () functions.d:13: Error: expected 1 function arguments, not 0 functions.d:13: Error: function functions.g (int function(int a) p) is not callable using argument types (int) functions.d:13: Error: cannot implicitly convert expression (f()) of type int to int function(int a)Use "delegate" or "function" both for the argument type and return type.
Apr 11 2012
On Wed, 11 Apr 2012 08:08:44 -0400, Xan <xancorreu gmail.com> wrote:On Wednesday, 11 April 2012 at 11:59:14 UTC, Jacob Carlborg wrote:int function(int) g(int function(int a) p) { return p; } Should do the trick. delegates are not implicitly convertible to/from function pointers. You can, however, explicitly convert a function to a delegate. But this should be done only when there is a requirement to use delegates. However, use std.functional.toDelegate if you are interested. -SteveUse "delegate" or "function" both for the argument type and return type.How to do that?
Apr 11 2012
On Wednesday, 11 April 2012 at 12:19:06 UTC, Steven Schveighoffer wrote:On Wed, 11 Apr 2012 08:08:44 -0400, Xan <xancorreu gmail.com> wrote:Thanks, Steve, but another problem: $ gdmd-4.6 functions.d functions.d:13: Error: function functions.f (int a) is not callable using argument types () functions.d:13: Error: expected 1 function arguments, not 0 functions.d:13: Error: function functions.g (int function(int) p) is not callable using argument types (int) functions.d:13: Error: cannot implicitly convert expression (f()) of type int to int function(int) import std.functional, std.stdio; int f (int a) { |___return 2*a; } int function(int) g(int function(int a) p) { |___return p; } void main() { |___writeln(g(f)(1)); } I want to g return f and then f evaluate 1.On Wednesday, 11 April 2012 at 11:59:14 UTC, Jacob Carlborg wrote:int function(int) g(int function(int a) p) { return p; } Should do the trick. delegates are not implicitly convertible to/from function pointers. You can, however, explicitly convert a function to a delegate. But this should be done only when there is a requirement to use delegates. However, use std.functional.toDelegate if you are interested. -SteveUse "delegate" or "function" both for the argument type and return type.How to do that?
Apr 11 2012
On Wed, 11 Apr 2012 08:53:00 -0400, Xan <xancorreu gmail.com> wrote:Thanks, Steve, but another problem:[snip]void main() { |___writeln(g(f)(1)); }writeln(g(&f)(1)); Unlike C, you *must* take the address of a function symbol to get a function pointer. -Steve
Apr 11 2012
On Wednesday, 11 April 2012 at 13:04:01 UTC, Steven Schveighoffer wrote:On Wed, 11 Apr 2012 08:53:00 -0400, Xan <xancorreu gmail.com> wrote:Yes, it works, finally. Thanks, Steve. Xan.Thanks, Steve, but another problem:[snip]void main() { |___writeln(g(f)(1)); }writeln(g(&f)(1)); Unlike C, you *must* take the address of a function symbol to get a function pointer. -Steve
Apr 11 2012
* Xan <xancorreu gmail.com> [2012-04-11 20:28:38 +0200]:On Wednesday, 11 April 2012 at 13:04:01 UTC, Steven Schveighoffer wrote:[snip]On Wed, 11 Apr 2012 08:53:00 -0400, Xan <xancorreu gmail.com> wrote:Glad you got help Xan, but for future reference can you please keep questions to D.learn? It is somewhat frustrating to see the question "How do I do that?" on this list, since it is for discussion, and high-level questions, not for people that are learning D. (High level questions being along the lines of "What changes need to be made in dmd to support the new AA implementation?") -- James Millerwriteln(g(&f)(1)); Unlike C, you *must* take the address of a function symbol to get a function pointer. -SteveYes, it works, finally. Thanks, Steve. Xan.
Apr 12 2012
On Thu, 12 Apr 2012 10:49:03 -0400, James Miller <james aatch.net> wrote:Glad you got help Xan, but for future reference can you please keep questions to D.learn? It is somewhat frustrating to see the question "How do I do that?" on this list, since it is for discussion, and high-level questions, not for people that are learning D.Um... this is d.learn... -Steve
Apr 12 2012
* James Miller <james aatch.net> [2012-04-13 02:49:03 +1200]:Glad you got help Xan, but for future reference can you please keep questions to D.learn? It is somewhat frustrating to see the question "How do I do that?" on this list, since it is for discussion, and high-level questions, not for people that are learning D. (High level questions being along the lines of "What changes need to be made in dmd to support the new AA implementation?")And now I look like an asshole because I wasn't paying attention to the mailbox I was in. Ignore me, I'm an idiot... -- James Miller
Apr 12 2012