"self" keyword

Rick C. Hodgin foxmuldrster@yahoo.com
Thu Jun 14 20:35:00 GMT 2012


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.  Likewise, the wrapper could be modified without having to 
concern itself with anything in the generated code, save some 
requirements of an API like a "print_notice()" or "print_error()" 
message function, which could just be a requirement of the app to always 
be there.  The rest, however, could be fluid.

A few other uses I can think of as well.  Minor ones.

Best regards,
Rick C. Hodgin

On 06/14/2012 04:24 PM, David Brown wrote:
> On 14/06/12 19:31, Joe Buck wrote:
>> It only saves one character in any case: your "self" is just "*this".
>
> No, "this" points to the object in C++.  The OP's "self" is referring 
> to the function being compiled.  So here "self" would be the same as 
> "foo".
>
> I don't think there is any way to get this without making a language 
> extension, unless there is some way of turning the string __FUNC__ 
> into the function.  But I also don't see any advantage over simply 
> using the function name directly.  After all, how often do you need 
> recursion - and what is the problem with writing out the function name 
> in full on those occasions?
>
> mvh.,
>
> David
>
>
>
>>
>> ________________________________________
>> From: gcc-owner@gcc.gnu.org [gcc-owner@gcc.gnu.org] on behalf of Ian 
>> Lance Taylor [iant@google.com]
>> Sent: Thursday, June 14, 2012 10:19 AM
>> To: Rick C. Hodgin
>> Cc: gcc@gcc.gnu.org
>> Subject: Re: "self" keyword
>>
>> "Rick C. Hodgin"<foxmuldrster@yahoo.com>  writes:
>>
>>> I was thinking C and C++.
>>>
>>> int myclass::foo(int a)
>>> {
>>>      // recursion
>>>      self(a + 1);
>>> }
>>>
>>> Just out of curiosity, why wouldn't it be accepted back into mainline?
>>
>> In general these days GCC discourages language extensions.  They would
>> have to have a compelling advantage.  I don't see that here.  Even if I
>> did, I would recommend running it through a language standards body
>> first.
>>
>> Ian
>>
>
>



More information about the Gcc mailing list