"self" keyword

Rick C. Hodgin foxmuldrster@yahoo.com
Fri Jun 15 12:35:00 GMT 2012


That would work. Yet now I'm back to remembering to update that line of code equating self to its function name at each use.

My desire for "self" as a keyword is in looking for a way to use contextual information the compiler already knows about and can easily employ.

Best regards,
Rick C. Hodgin

-------- Original Message --------
 From: Václav Zeman <vhaisman@gmail.com>
 Sent: Fri, Jun 15, 2012 08:08 AM
 To: Oleg Endo <oleg.endo@t-online.de>
 CC: Rick C. Hodgin <foxmuldrster@yahoo.com>; David Brown <david.brown@hesbynett.no>; 	Joe Buck <Joe.Buck@synopsys.com>; Ian Lance Taylor <iant@google.com>; 	gcc@gcc.gnu.org <gcc@gcc.gnu.org>
 Subject: Re: "self" keyword

>On 14 June 2012 22:42, Oleg Endo wrote:
>> On Thu, 2012-06-14 at 16:34 -0400, Rick C. Hodgin wrote:
>>> David,
>>>
>>> Well, I probably don't have a NEED for it.  I've gotten along for 25+
>>> years without it. :-)
>>>
>>> However, what prompted my inquiry is using it would've saved me tracking
>>> down a few bugs in recent weeks.  Some prior code was re-used for a
>>> similar function, but the name of the recursive calls weren't updated in
>>> every case.  It didn't take long to debug, but I realized that had it
>>> always been written as self() it never would've been an issue.
>>>
>>> I can also see a use for generated code where there's a base source code
>>> template in use with an embedded include file reference that changes as
>>> it's generated per pass, such as:
>>>
>>> int step1(int a, int b)
>>> {
>>>      #include "\current_task\step1.cpp"
>>> }
>>>
>>> int step2(int a, int b)
>>> {
>>>      #include "\current_task\step2.cpp"
>>> }
>>>
>>> Using the self() reference for recursion, one could modify stepN.cpp's
>>> generator algorithms without having to know or care anything in the
>>> wrapper code.
>>
>> Wouldn't this do?
>>
>> #define __self__ step1
>> int __self__ (int a, int b)
>> {
>>  #include "something"
>>  __self__ (x, y);
>> }
>> #undef __self__
>You can already do this with GCC in C and C++ (minus problems with
>overloaded functions) like this:
>
>#define DECLSELF(f,self) __typeof__ (&f) self = f
>
>int foo (int n)
>{
>    DECLSELF(foo, self);
>
>    if (n == 0)
>        return 0;
>    else
>    {
>        return 1 + self (n - 1);
>    }
>}
>
>-- 
>VZ


More information about the Gcc mailing list