[Proposal] Syntax sugar/operator/keyword for "debug.getinfo(1, 'f').func()" (or just debug.getinfo(1,'f').func)

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[Proposal] Syntax sugar/operator/keyword for "debug.getinfo(1, 'f').func()" (or just debug.getinfo(1,'f').func)

Soni "They/Them" L.
Hi,

I had this function:

local function myfunc(a,b)
  -- some code here
  x = myfunc(y,z)
  -- more code here
  return v
end

Then I changed the name of the function:

local function dothing(a,b)

But I forgot to change the body!

To avoid having to change the function body every time (and to be able to call itself when using local name = function(args)), I propose an operator or keyword or something that returns the same value as "debug.getinfo(1,'f').func"...

"What should it be?" good question... I'm not sure... I'd use "self" but that's reserved by the :func() syntax sugar... "function.self" seems interesting but weird/'not-lua'...
-- 
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.
Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] Syntax sugar/operator/keyword for "debug.getinfo(1, 'f').func()" (or just debug.getinfo(1,'f').func)

Steven Degutis
Quoth SoniEx2:

> To avoid having to change the function body every time (and to be able to
> call itself when using local name = function(args)), I propose an operator
> or keyword or something that returns the same value as
> "debug.getinfo(1,'f').func"...

Most functions aren't recursive, and of those that are, they usually
have a very small, specialized task, so that it rarely needs to be
renamed. I doubt even 1% of future code would use your proposed
operator/keyword. But if you find this feature useful for your
situation, you could just turn that into a function and store it as
debug.thisfunction, which would allow all your code to access it.

-Steven

Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] Syntax sugar/operator/keyword for "debug.getinfo(1, 'f').func()" (or just debug.getinfo(1, 'f').func)

Sean Conner
In reply to this post by Soni "They/Them" L.
It was thus said that the Great Thiago L. once stated:

> Hi,
>
> I had this function:
>
> local function myfunc(a,b)
>   -- some code here
>   x = myfunc(y,z)
>   -- more code here
>   return v
> end
>
> Then I changed the name of the function:
>
> local function dothing(a,b)
>
> But I forgot to change the body!
>
> To avoid having to change the function body every time (and to be able
> to call itself when using local name = function(args)), I propose an
> operator or keyword or something that returns the same value as
> "debug.getinfo(1,'f').func"...
>
> "What should it be?" good question... I'm not sure... I'd use "self" but
> that's reserved by the :func() syntax sugar... "function.self" seems
> interesting but weird/'not-lua'...


  I think what you want is known as the Y-combinator.  

function Y(f)
  local function g(...) return f(g,...) end
  return g
end

local dothing = Y(function(me,a,b)
  -- some code here
  x = me(a,b)
  -- more code here
  return y
end)

--oh, and ...

print(Y(function(rec, x) if x < 2 then return 1 else return x * rec(x-1) end end)(5))

  -spc (Or you could define Y() to call debug.getinfo(2,'f').func for you ... )

Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] Syntax sugar/operator/keyword for "debug.getinfo(1, 'f').func()" (or just debug.getinfo(1, 'f').func)

Tim Hill
In reply to this post by Soni "They/Them" L.

On Nov 26, 2014, at 4:04 PM, Thiago L. <[hidden email]> wrote:

Hi,

I had this function:

local function myfunc(a,b)
  -- some code here
  x = myfunc(y,z)
  -- more code here
  return v
end

Then I changed the name of the function:

local function dothing(a,b)

But I forgot to change the body!

To avoid having to change the function body every time (and to be able to call itself when using local name = function(args)), I propose an operator or keyword or something that returns the same value as "debug.getinfo(1,'f').func"...

"What should it be?" good question... I'm not sure... I'd use "self" but that's reserved by the :func() syntax sugar... "function.self" seems interesting but weird/'not-lua'...


Wrap the function and let it capture itself as an upvalue. For example;

do
local X = function(...)
    X(...)
end
someglobal = X
end

No need for any syntactic sugar

Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] Syntax sugar/operator/keyword for "debug.getinfo(1, 'f').func()" (or just debug.getinfo(1, 'f').func)

Soni "They/Them" L.

On 27/11/14 02:58 AM, Tim Hill wrote:

On Nov 26, 2014, at 4:04 PM, Thiago L. <[hidden email]> wrote:

Hi,

I had this function:

local function myfunc(a,b)
  -- some code here
  x = myfunc(y,z)
  -- more code here
  return v
end

Then I changed the name of the function:

local function dothing(a,b)

But I forgot to change the body!

To avoid having to change the function body every time (and to be able to call itself when using local name = function(args)), I propose an operator or keyword or something that returns the same value as "debug.getinfo(1,'f').func"...

"What should it be?" good question... I'm not sure... I'd use "self" but that's reserved by the :func() syntax sugar... "function.self" seems interesting but weird/'not-lua'...

      

Wrap the function and let it capture itself as an upvalue. For example;

do
local X = function(...)
doesn't work
    X(...)
end
someglobal = X
end

No need for any syntactic sugar


-- 
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.
Reply | Threaded
Open this post in threaded view
|

Re: [Proposal] Syntax sugar/operator/keyword for "debug.getinfo(1, 'f').func()" (or just debug.getinfo(1, 'f').func)

Tim Hill

>>
>> Wrap the function and let it capture itself as an upvalue. For example;
>>
>> do
>> local X = function(...)
> doesn't work
>>     X(...)
>> end
>> someglobal = X
>> end
>>
>> No need for any syntactic sugar
>>
>


I meant this of course…

do
        local function X(i)
                if i == 1 then return 1 end
                return X(i - 1) * i
        end
        fact = X
end
print(fact(3))

—Tim