[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Mutual recursion
- From: Martin <dyngeccetor8@...>
- Date: Thu, 23 Apr 2020 23:25:44 +0200
On 4/23/20 1:40 PM, Gavin Wraith wrote:
I have an idle question, which I thought I would put to
the list in case anybody had come up with a slick answer.
At present the only way to implement mutual recursion in
Lua is to declare two local variables first within a chunk
and then to define two mutually dependent functions later
in that chunk with the same names. There is no way to have
mutual recursion between functions defined in separate files,
for example. But is there a way to mimic a linker, using
loadfile, and then doing some patching?
I hope this proposition does not seem too indelicate to
those of refined senses.
Gavin Wraith (email@example.com)
Home page: http://www.wra1th.plus.com/
I believe you can solve a lot of problems with hammer, duct tape
and Lua "debug" module.
Here is my artificial example of mutually dependent functions
placed in separate chunks. Code prints binary representation
of given integer in reverse order. print_digit() prints last digit
and calls do_shift(). Which shifts number by one position and
To avoid cycle at require() time we only allocate "do_shift.print_digit"
slot and set it to real value after require() is finished.
--[[ main.lua ]]--
local print_binary = require('print_digit')
--[[ print_digit.lua ]]--
local do_shift = require('do_shift')
local result =
if (n <= 0) then
print(n % 2)
local set_upval =
function(chunk, name, value)
local offs = 0
offs = offs + 1
local k, v = debug.getupvalue(chunk, offs)
until (k == name)
debug.setupvalue(do_shift, offs, value)
-- Patch do_shift() by setting "print_digit" to us.
set_upval(do_shift, 'print_digit', result)
--[[ do_shift.lua ]]--
Can't "require('print_digit')" due cyclic reference.
We'll patch "print_digit" value later from caller.
n = n // 2
lua-l mailing list -- firstname.lastname@example.org
To unsubscribe send an email to email@example.com