• Subject: Re: Assigning to a constant variable with function definition syntax doesn't error
• From: Francisco Olarte <folarte@...>
• Date: Fri, 18 Jun 2021 09:46:18 +0200

```Sean:

On Fri, Jun 18, 2021 at 3:10 AM Sean Conner <sean@conman.org> wrote:
> local function Y(f)
...
> local y <const> = Y(function(r,x)

You do not have to go thorugh this:
-------------
\$ cat tmp/recur.lua; lua tmp/recur.lua
local r <const> = (
function()
local function r1(x)
print('r1',x)
if (x>0) then
return r1(x-1)
end
end
return r1
end)()
r(2)

r1    2
r1    1
r1    0
-------------
I tested it looking for a pattern if needed, first you develop the
normal function without const using a temporary name, then you wrap
the definition  with the "local real_name <const> = ( function() " +
\$prevcode + " return tmp_name end)()" after testing. I think debug
will probably show the temp name somewhere, but given this is only
needed for self-referential local const funtions I doubt it's too much
of a problem. It can even work for mutually recursive functions:
-----
\$ cat tmp/mutrec.lua; lua tmp/mutrec.lua
local a <const>, b <const> = (
function()
local b1;
local function a1(x)
print('a',x)
if (x>0) then
return b1(x-1)
end
end
b1= function (x)
print('b',x)
if (x>0) then
return a1(x-1)
end
end
return a1,b1
end)()
a(2)
b(2)

a    2
b    1
a    0
b    2
a    1
b    0
---------

Francisco Olarte.

```

• References: