reference to 'self' inside a function
Era Scarecrow
rtcvb32 at yahoo.com
Tue Jul 17 15:47:49 PDT 2012
On Tuesday, 17 July 2012 at 22:13:13 UTC, Eyyub wrote:
> On Tuesday, 17 July 2012 at 16:56:17 UTC, angel wrote:
>> I propose to introduce a reference to the current function,
>> much like 'this' in a class method. Call it 'self' or
>> 'thisFunc', or whatever ...
>> What might this be good for ?
>> For implementing recursion in a lambda function. Writing in
>> functional style, one creates many functions, and looking for
>> reasonable names for these functions becomes unnecessarily
>> painful.
>
> Good idea !
> "self" is a cute keyword, or "lambda" but this could break
> existing code.
Mmm.. Why not an inner function to represent the recursive
function? True a 'self' reference may resolve the issue, and be
syntactical sugar..
auto f = function int(int x) {
//real function body
void self(int y) {
if(y) {
inner(y-1);
x++;
}
}
self(x); //double x
self(x); //double it again
return x;
}; //end of f declaration
writeln(10);
writeln(f(10)); //10*4
Mmm But using it as a shell although may work wouldn't be useful
for a simple lambda anymore would it? Who knows, perhaps 'this'
will extend to lambda's referencing itself.
Using CTFE you could rebuild and get something similar to that,
or a template function... Hmmmm... Something to think on...
More information about the Digitalmars-d
mailing list