digitalmars.D.learn - Is there a way for a function to return itself in D?
- "Simen Kjaeraas" <simen.kjaras gmail.com> Oct 14 2008
- Lars Kyllingstad <public kyllingen.NOSPAMnet> Oct 14 2008
- Lars Kyllingstad <public kyllingen.NOSPAMnet> Oct 14 2008
- Janderson <ask me.com> Oct 14 2008
- "Steven Schveighoffer" <schveiguy yahoo.com> Oct 14 2008
- BCS <ao pathlink.com> Oct 14 2008
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: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
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. -Joel
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









Lars Kyllingstad <public kyllingen.NOSPAMnet> 