digitalmars.D.learn - Is there a way for a function to return itself in D?
- Simen Kjaeraas (7/7) Oct 14 2008 Like the subject says, I wonder if it's possible for a function to retur...
- Lars Kyllingstad (22/27) Oct 14 2008 Something like this?
- Lars Kyllingstad (5/16) Oct 14 2008 Ah, I just thought of the obvious use case: Chaining function calls. In
- Janderson (7/44) Oct 14 2008 This could be useful if you are generating a stack of operations that
- Steven Schveighoffer (14/19) Oct 14 2008 Nope. Because the type of the function must include all the functions y...
- BCS (6/13) Oct 14 2008 struct Fwrap(T...)
Like the subject says, I wonder if it's possible for a function to return itself. No matter how I think of this, I end up with an endless recursive function declaration. Creating a functor that does this is simple, so why not a function? -- Simen
Oct 14 2008
Simen Kjaeraas wrote:Like the subject says, I wonder if it's possible for a function to return itself. No matter how I think of this, I end up with an endless recursive function declaration. Creating a functor that does this is simple, so why not a function?Something like this? import tango.io.Stdout; void* returnSelf() { Stdout("Hello world!").newline; return cast(void*) &returnSelf; } Then the function can be called like this: void main() { void* function() rs; // This prints "Hello world!" rs = cast(void* function()) returnSelf(); // This prints "Hello world!" too. rs(); } But it's kinda ugly, what with the casts and all. I don't see that it is useful either. It makes sense to return a functor, because it is an object and has state. A function is stateless, so any "reference" to the function will be identical. -Lars
Oct 14 2008
Lars Kyllingstad wrote:Simen Kjaeraas wrote:[...]Like the subject says, I wonder if it's possible for a function to return itself. No matter how I think of this, I end up with an endless recursive function declaration. Creating a functor that does this is simple, so why not a function?But it's kinda ugly, what with the casts and all. I don't see that it is useful either. It makes sense to return a functor, because it is an object and has state. A function is stateless, so any "reference" to the function will be identical.Ah, I just thought of the obvious use case: Chaining function calls. In which case my previous post won't be useful at all. -Lars
Oct 14 2008
Lars Kyllingstad wrote:Simen Kjaeraas wrote:This could be useful if you are generating a stack of operations that can be undone (or modified). Although in that case you could just as easily have a helper function that would push the function pointer and call the function, rather then having to add that syntax to every operation that goes into the stack. -JoelLike the subject says, I wonder if it's possible for a function to return itself. No matter how I think of this, I end up with an endless recursive function declaration. Creating a functor that does this is simple, so why not a function?Something like this? import tango.io.Stdout; void* returnSelf() { Stdout("Hello world!").newline; return cast(void*) &returnSelf; } Then the function can be called like this: void main() { void* function() rs; // This prints "Hello world!" rs = cast(void* function()) returnSelf(); // This prints "Hello world!" too. rs(); } But it's kinda ugly, what with the casts and all. I don't see that it is useful either. It makes sense to return a functor, because it is an object and has state. A function is stateless, so any "reference" to the function will be identical. -Lars
Oct 14 2008
"Simen Kjaeraas" <simen.kjaras gmail.com> wrote in message news:op.ui0ff1np1hx7vj biotronic.osir.hihm.no...Like the subject says, I wonder if it's possible for a function to return itself. No matter how I think of this, I end up with an endless recursive function declaration. Creating a functor that does this is simple, so why not a function?Nope. Because the type of the function must include all the functions you might call. i.e. function(int x) function(int x) is a different type than function(int x) function(int x) function(int x) For a functor it's simple. You are not returning a function, but a pointer to a possibly not fully-defined object, so the type does not need to be fully specified before it is used. You would need to be able to declare a function type before defining it in order to be able to do this. -Steve
Oct 14 2008
Reply to Simen,Like the subject says, I wonder if it's possible for a function to return itself. No matter how I think of this, I end up with an endless recursive function declaration. Creating a functor that does this is simple, so why not a function?struct Fwrap(T...) { Fwrap!(T) function(T) inner; Fwrap!(T) opCall(T t){ return inner(t); } //optional }
Oct 14 2008