View mode: basic / threaded / horizontal-split · Log in · Help
July 17, 2012
reference to 'self' inside a function
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.
July 17, 2012
Re: reference to 'self' inside a function
On Jul 17, 2012 1:00 PM, "angel" <andrey.gelman@gmail.com> 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.

I like that idea, although I don't know about the name.  We also might want
more features such as access to syntacticly nested functions.
July 17, 2012
Re: reference to 'self' inside a function
On Tue, Jul 17, 2012 at 01:09:09PM -0400, Kevin Cox wrote:
> On Jul 17, 2012 1:00 PM, "angel" <andrey.gelman@gmail.com> 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.
> 
> I like that idea, although I don't know about the name.  We also might
> want more features such as access to syntacticly nested functions.

We certainly can't use 'this', because it will break lambda functions
declared inside class methods. So 'self' seems like a reasonable
alternative. But this requires introducing a new keyword, which means
there's the likelihood of breaking existing code that happens to use an
identifier called 'self'.

It's a good idea, though. It will strengthen D's support for functional
style code.


T

-- 
Let's eat some disquits while we format the biskettes.
July 17, 2012
Re: reference to 'self' inside a function
On 07/17/2012 06:56 PM, 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.

I think it is not the naming that is painful. (I am not aware of
functional languages that have a special identifier for the current
anonymous function.)

What makes writing short named functions inconvenient is lack of this:
http://d.puremagic.com/issues/show_bug.cgi?id=7176
July 17, 2012
Re: reference to 'self' inside a function
angel:

> I propose to introduce a reference to the current function, 
> much like 'this' in a class method. Call it 'self' or 
> 'thisFunc', or whatever ...

Or __func or something. I'm asking for it for years.

Bye,
bearophile
July 17, 2012
Re: reference to 'self' inside a function
Other languages provide other means.
In any case, I don't think it would be a problem if D solved a 
problem other languages failed to resolve properly.
As for  http://d.puremagic.com/issues/show_bug.cgi?id=7176, this 
is pretty much orthogonal to the 'self reference'.
July 17, 2012
Re: reference to 'self' inside a function
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.
July 17, 2012
Re: reference to 'self' inside a function
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...
July 17, 2012
Re: reference to 'self' inside a function
On Jul 17, 2012 6:50 PM, "Era Scarecrow" <rtcvb32@yahoo.com> wrote:
>
> 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,

What about how JavaScript does it.  Anonymous functions can still have a
"name" that can be used from inside of a function to refer to itself.

And the most useless example ever.

var f = function func ( i ) {
   return func(7);
};

I think that this is nice because there is no name space pollution, no new
keywords needed and it is pretty.
July 18, 2012
Re: reference to 'self' inside a function
On Wednesday, July 18, 2012 00:03:25 bearophile wrote:
> angel:
> > I propose to introduce a reference to the current function,
> > much like 'this' in a class method. Call it 'self' or
> > 'thisFunc', or whatever ...
> 
> Or __func or something. I'm asking for it for years.

You could always use __FUNCTION__ in a string mixin, but that _is_ rather 
ugly.

- Jonathan M Davis
« First   ‹ Prev
1 2 3 4
Top | Discussion index | About this forum | D home