Re: Can I pass a function by parameter?

2014-09-08 Thread deadalnix via Digitalmars-d
On Monday, 8 September 2014 at 05:04:21 UTC, Jakob Ovrum wrote: Function pointers can be converted to delegates through a simple wrapper function (which is how std.functional.toDelegate achieves it), but delegates cannot be wrapped by a function pointer without introducing additional

Re: Can I pass a function by parameter?

2014-09-08 Thread Jakob Ovrum via Digitalmars-d
On Monday, 8 September 2014 at 06:23:40 UTC, deadalnix wrote: On Monday, 8 September 2014 at 05:04:21 UTC, Jakob Ovrum wrote: Function pointers can be converted to delegates through a simple wrapper function (which is how std.functional.toDelegate achieves it), but delegates cannot be wrapped

Can I pass a function by parameter?

2014-09-07 Thread AsmMan via Digitalmars-d
I'm trying to use a bit of function programming. In a function like this: int f(in int[] arr, bool delegate(int) func); call using: bool g(int n) { ... } f(arr, g); instead of: f(arr, x = x == 0); it is possible?

Re: Can I pass a function by parameter?

2014-09-07 Thread John Colvin via Digitalmars-d
On Sunday, 7 September 2014 at 20:47:47 UTC, AsmMan wrote: I'm trying to use a bit of function programming. In a function like this: int f(in int[] arr, bool delegate(int) func); call using: bool g(int n) { ... } f(arr, g); instead of: f(arr, x = x == 0); it is possible? bool g(int n) {

Re: Can I pass a function by parameter?

2014-09-07 Thread Jakob Ovrum via Digitalmars-d
On Sunday, 7 September 2014 at 21:02:16 UTC, John Colvin wrote: bool g(int n) { ... } f(arr, g); This will fail if `g` is a function pointer, such as when `g` is declared at module-level scope. In that case, it has to be explicitly converted to a delegate: --- import std.functional :

Re: Can I pass a function by parameter?

2014-09-07 Thread AsmMan via Digitalmars-d
On Sunday, 7 September 2014 at 21:02:16 UTC, John Colvin wrote: On Sunday, 7 September 2014 at 20:47:47 UTC, AsmMan wrote: I'm trying to use a bit of function programming. In a function like this: int f(in int[] arr, bool delegate(int) func); call using: bool g(int n) { ... } f(arr, g);

Re: Can I pass a function by parameter?

2014-09-07 Thread AsmMan via Digitalmars-d
On Sunday, 7 September 2014 at 21:23:26 UTC, Jakob Ovrum wrote: On Sunday, 7 September 2014 at 21:02:16 UTC, John Colvin wrote: bool g(int n) { ... } f(arr, g); This will fail if `g` is a function pointer, such as when `g` is declared at module-level scope. In that case, it has to be

Re: Can I pass a function by parameter?

2014-09-07 Thread Jakob Ovrum via Digitalmars-d
On Sunday, 7 September 2014 at 21:31:11 UTC, AsmMan wrote: Thank you too. Btw, why the operator in this syntax? I used to think ref keyword sort of C's T** and operator is neeeded.. or is it because f can be a function called without pass any parameter? In D, the address-of operator has to

Re: Can I pass a function by parameter?

2014-09-07 Thread Jakob Ovrum via Digitalmars-d
On Sunday, 7 September 2014 at 21:42:31 UTC, Jakob Ovrum wrote: void bar(void function() a) {} s/void function() a/int function() a/

Re: Can I pass a function by parameter?

2014-09-07 Thread AsmMan via Digitalmars-d
On Sunday, 7 September 2014 at 21:42:31 UTC, Jakob Ovrum wrote: On Sunday, 7 September 2014 at 21:31:11 UTC, AsmMan wrote: Thank you too. Btw, why the operator in this syntax? I used to think ref keyword sort of C's T** and operator is neeeded.. or is it because f can be a function called

Re: Can I pass a function by parameter?

2014-09-07 Thread deadalnix via Digitalmars-d
On Sunday, 7 September 2014 at 21:42:31 UTC, Jakob Ovrum wrote: On Sunday, 7 September 2014 at 21:31:11 UTC, AsmMan wrote: Thank you too. Btw, why the operator in this syntax? I used to think ref keyword sort of C's T** and operator is neeeded.. or is it because f can be a function called

Re: Can I pass a function by parameter?

2014-09-07 Thread deadalnix via Digitalmars-d
On Sunday, 7 September 2014 at 20:47:47 UTC, AsmMan wrote: I'm trying to use a bit of function programming. In a function like this: int f(in int[] arr, bool delegate(int) func); call using: bool g(int n) { ... } f(arr, g); instead of: f(arr, x = x == 0); it is possible? In D there is a

Re: Can I pass a function by parameter?

2014-09-07 Thread Jakob Ovrum via Digitalmars-d
On Monday, 8 September 2014 at 03:37:45 UTC, deadalnix wrote: In fact the distinction is done by C and C++ only. And by D.

Re: Can I pass a function by parameter?

2014-09-07 Thread Jakob Ovrum via Digitalmars-d
On Monday, 8 September 2014 at 03:01:40 UTC, AsmMan wrote: I got it. Why it doesn't works if foo is a method? Taking the address of a method/member function yields a delegate, not a function pointer. Delegates are fat pointers; they contain a pointer to the function as well as a pointer to