www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Self function

reply bearophile <bearophileHUGS lycos.com> writes:
Sometimes I rename recursive functions, or I duplicate and modify them, and
they stop working because inside them there's one or more copy of their old
name, so for example they recurse to their old name.
So inside a function I'd like to have a standard name to call the function
itself, useful for recursivity.
(If you have two or more recursive functions that call each other this idea
can't be used, but I think such situations are uncommon enough to not deserve
help from the language).

I have just discussed this in the Python newsgroup too:


I use more recursivity in D than in Python, because Python has troubles with it.

In future in D2 you may use:

int ANUGLYNAME(int n) {
    if (n <= 1)
        return 1;
    else
        mixin(__FUNCTION__ ~ "(n - 1) * n");
}

But you can't use __FUNCTION__ into a delegate/function pointer/lambda because
the name isn't available, and it's a bit ugly syntax anyway...

This looks a bit better:

int ANUGLYNAME(int n) {
    if (n <= 1)
        return 1;
    else
        __self(n - 1) * n;
}

Other syntaxes are possible.

__self is a way to denote the pointer/delegate of the function currently being
run, so I think the compiler is always able to that, for delegate/ function
pointers/ lambdas/ methods/ virtual methods/ opCalls too.

Bye,
bearophile
May 04 2009
next sibling parent Frank Benoit <keinfarbton googlemail.com> writes:
bearophile schrieb:
 Sometimes I rename recursive functions, or I duplicate and modify them, and
they stop working because inside them there's one or more copy of their old
name, so for example they recurse to their old name.
 So inside a function I'd like to have a standard name to call the function
itself, useful for recursivity.
 (If you have two or more recursive functions that call each other this idea
can't be used, but I think such situations are uncommon enough to not deserve
help from the language).
 
 I have just discussed this in the Python newsgroup too:

 
 I use more recursivity in D than in Python, because Python has troubles with
it.
 
 In future in D2 you may use:
 
 int ANUGLYNAME(int n) {
     if (n <= 1)
         return 1;
     else
         mixin(__FUNCTION__ ~ "(n - 1) * n");
 }
 
 But you can't use __FUNCTION__ into a delegate/function pointer/lambda because
the name isn't available, and it's a bit ugly syntax anyway...
 
 This looks a bit better:
 
 int ANUGLYNAME(int n) {
     if (n <= 1)
         return 1;
     else
         __self(n - 1) * n;
 }
 
 Other syntaxes are possible.
 
 __self is a way to denote the pointer/delegate of the function currently being
run, so I think the compiler is always able to that, for delegate/ function
pointers/ lambdas/ methods/ virtual methods/ opCalls too.
 
 Bye,
 bearophile
how about scope.function // the surrounding function scope.method // the surrounding method scope.class // the surrounding class scope.class.outer // the outer class of the surrounding class The current functions name, was requested so often: scope.function.name ?
May 04 2009
prev sibling next sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Mon, 04 May 2009 23:52:56 +0400, bearophile <bearophileHUGS lycos.com> wrote:

 Sometimes I rename recursive functions, or I duplicate and modify them,  
 and they stop working because inside them there's one or more copy of  
 their old name, so for example they recurse to their old name.
 So inside a function I'd like to have a standard name to call the  
 function itself, useful for recursivity.
 (If you have two or more recursive functions that call each other this  
 idea can't be used, but I think such situations are uncommon enough to  
 not deserve help from the language).

 I have just discussed this in the Python newsgroup too:


 I use more recursivity in D than in Python, because Python has troubles  
 with it.

 In future in D2 you may use:

 int ANUGLYNAME(int n) {
     if (n <= 1)
         return 1;
     else
         mixin(__FUNCTION__ ~ "(n - 1) * n");
 }

 But you can't use __FUNCTION__ into a delegate/function pointer/lambda  
 because the name isn't available, and it's a bit ugly syntax anyway...

 This looks a bit better:

 int ANUGLYNAME(int n) {
     if (n <= 1)
         return 1;
     else
         __self(n - 1) * n;
 }

 Other syntaxes are possible.

 __self is a way to denote the pointer/delegate of the function currently  
 being run, so I think the compiler is always able to that, for delegate/  
 function pointers/ lambdas/ methods/ virtual methods/ opCalls too.

 Bye,
 bearophile
It was proposed awhile ago: http://www.digitalmars.com/d/archives/digitalmars/D/FUNCTION_84985.html Andrei's respond: "I think instead of __FUNCTION__ we'll define a much more comprehensive static reflection facility." "You will have it as a human readable identifier too. The problem with __FUNCTION__, __CLASS__ etc. is that the list of ad-hoc names (what happened to __STRUCT__, __MODULE__ et al?) can go forever." "D2 will have reflection. (Walter doesn't know yet. He thinks D3 will have reflection.) It will be compile-time reflection because only run-time reflection only is missing the point." "This is a long discussion, but in brief any runtime reflection engine needs some sort of compile-time metadata infrastructure. Some languages don't make that accessible within the language itself. Runtime reflection has been explored extensively, its possibilities and limitations are pretty well understood, I hardly smothered a yawn reading all you wrote. Compile-time reflection is much less understood and hides many more exciting possibilities. With the advances in compiler technology implemented by Walter, we have a chance to tackle reflection in a systematic manner."
May 04 2009
parent reply bearophile <bearophileHUGS lycos.com> writes:
Denis Koroskin:
 It was proposed awhile ago:
http://www.digitalmars.com/d/archives/digitalmars/D/FUNCTION_84985.html
I am not asking for a static __function__ name, because you can't use it inside lambdas or function pointers/delegates. The __func I am talking about is a pointer/delegate, and I don't know if you can use a static solution once you start juggling function pointers around. Bye, bearophile
May 04 2009
parent "Denis Koroskin" <2korden gmail.com> writes:
On Tue, 05 May 2009 02:30:49 +0400, bearophile <bearophileHUGS lycos.com> wrote:

 Denis Koroskin:
 It was proposed awhile ago:  
 http://www.digitalmars.com/d/archives/digitalmars/D/FUNCTION_84985.html
I am not asking for a static __function__ name, because you can't use it inside lambdas or function pointers/delegates. The __func I am talking about is a pointer/delegate, and I don't know if you can use a static solution once you start juggling function pointers around. Bye, bearophile
If you read carefully, you'll see that there's more than just __FUNCTION__. For example, I suggested to use some literal to denote current function, fthis/self/whatever. Others suggested scope.function/scope.class/etc. Andrei agreed that there should be a compile-time reflection that would allow function to call itself in some generic way.
May 04 2009
prev sibling parent reply Georg Wrede <georg.wrede iki.fi> writes:
bearophile wrote:
 Sometimes I rename recursive functions, or I duplicate and modify
them, and they stop working because inside them there's one or more copy of their old name, so for example they recurse to their old name.
 So inside a function I'd like to have a standard name to call the
function itself, useful for recursivity.
 (If you have two or more recursive functions that call each other
 this
idea can't be used, but I think such situations are uncommon enough to not deserve help from the language).
 
 I have just discussed this in the Python newsgroup too:

 
 I use more recursivity in D than in Python, because Python has troubles with
it.
 
 In future in D2 you may use:
 
 int ANUGLYNAME(int n) {
     if (n <= 1)
         return 1;
     else
         mixin(__FUNCTION__ ~ "(n - 1) * n");
 }
 
 But you can't use __FUNCTION__ into a delegate/function
 pointer/lambdabecause the name isn't available, and it's a bit ugly
 syntax anyway...
 
 This looks a bit better:
 
 int ANUGLYNAME(int n) {
     if (n <= 1)
         return 1;
     else
         __self(n - 1) * n;
 }
 
 Other syntaxes are possible.
 
 __self is a way to denote the pointer/delegate of the function
 currently being run, so I think the compiler is always able to that, for
 delegate/ function pointers/ lambdas/ methods/ virtual methods/ opCalls too.
Since you need this at compile time, then you don't need a pointer. A name would be enough. If, as Denis pointed out, Andrei is going to provide that, and if it turns out to have a long name (like scope.function.name), then I hope it will be implemented so that you can alias that into something shorter, like "thisf" or "me".
May 04 2009
parent reply BCS <ao pathlink.com> writes:
Reply to Georg,

 bearophile wrote:
 
 Sometimes I rename recursive functions, or I duplicate and modify
 
them, and they stop working because inside them there's one or more copy of their old name, so for example they recurse to their old name.
 So inside a function I'd like to have a standard name to call the
 
function itself, useful for recursivity.
 (If you have two or more recursive functions that call each other
 this
 
idea can't be used, but I think such situations are uncommon enough to not deserve help from the language).
 I have just discussed this in the Python newsgroup too:
 http://groups.google.com/group/comp.lang.python/browse_thread/thread/

 
 I use more recursivity in D than in Python, because Python has
 troubles with it.
 
 In future in D2 you may use:
 
 int ANUGLYNAME(int n) {
 if (n <= 1)
 return 1;
 else
 mixin(__FUNCTION__ ~ "(n - 1) * n");
 }
 But you can't use __FUNCTION__ into a delegate/function
 pointer/lambdabecause the name isn't available, and it's a bit ugly
 syntax anyway...
 
 This looks a bit better:
 
 int ANUGLYNAME(int n) {
 if (n <= 1)
 return 1;
 else
 __self(n - 1) * n;
 }
 Other syntaxes are possible.
 
 __self is a way to denote the pointer/delegate of the function
 currently being run, so I think the compiler is always able to that,
 for
 delegate/ function pointers/ lambdas/ methods/ virtual methods/
 opCalls too.
Since you need this at compile time, then you don't need a pointer. A name would be enough. If, as Denis pointed out, Andrei is going to provide that, and if it turns out to have a long name (like scope.function.name), then I hope it will be implemented so that you can alias that into something shorter, like "thisf" or "me".
void main() { int i = 0; auto dg = (int j) { i++; return j <= 1 ? 1 : self(j-1) + self(j-2); // self can't be a name as the function dosn't have a name // and it can't be a compile time const at the context is not known. }; dg(5); }
May 04 2009
parent Georg Wrede <georg.wrede iki.fi> writes:
BCS wrote:
 Reply to Georg,
 
 bearophile wrote:

 Sometimes I rename recursive functions, or I duplicate and modify
them, and they stop working because inside them there's one or more copy of their old name, so for example they recurse to their old name.
 So inside a function I'd like to have a standard name to call the
function itself, useful for recursivity.
 (If you have two or more recursive functions that call each other
 this
idea can't be used, but I think such situations are uncommon enough to not deserve help from the language).
 I have just discussed this in the Python newsgroup too:
 http://groups.google.com/group/comp.lang.python/browse_thread/thread/


 I use more recursivity in D than in Python, because Python has
 troubles with it.

 In future in D2 you may use:

 int ANUGLYNAME(int n) {
 if (n <= 1)
 return 1;
 else
 mixin(__FUNCTION__ ~ "(n - 1) * n");
 }
 But you can't use __FUNCTION__ into a delegate/function
 pointer/lambdabecause the name isn't available, and it's a bit ugly
 syntax anyway...

 This looks a bit better:

 int ANUGLYNAME(int n) {
 if (n <= 1)
 return 1;
 else
 __self(n - 1) * n;
 }
 Other syntaxes are possible.

 __self is a way to denote the pointer/delegate of the function
 currently being run, so I think the compiler is always able to that,
 for
 delegate/ function pointers/ lambdas/ methods/ virtual methods/
 opCalls too.
Since you need this at compile time, then you don't need a pointer. A name would be enough. If, as Denis pointed out, Andrei is going to provide that, and if it turns out to have a long name (like scope.function.name), then I hope it will be implemented so that you can alias that into something shorter, like "thisf" or "me".
void main() { int i = 0; auto dg = (int j) { i++; return j <= 1 ? 1 : self(j-1) + self(j-2); // self can't be a name as the function dosn't have a name // and it can't be a compile time const at the context is not known. }; dg(5); }
Oh boy, I missed half of the post. (Note to self: remember use two eyes for reading. Especially at the wee hours.)
May 04 2009