lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


  For reasons, I have the following Lua code in a file:

	(function(s)
	  if #s > 0 then
	    print(s)
	    return debug.getinfo(1,'f').func(s:sub(1,math.floor(#s / 2)))
	  end
	end)(string.rep("-",64))
	
	;
	
	((function(f)
	  local function g(...) return f(g,...) end
	  return g
	end)(function(f,s)
	  if #s > 0 then
	    print(s)
	    return f(s:sub(1,math.floor(#s / 2)))
	  end
	end))(string.rep("+",64))

  These are two different ways of an anonymous function calling itself
recursively (the first is using the hack of calling debug.getinfo(), and the
second is using the Y-combinator).  When the code is run, using Lua 5.1 or
higher, the output is:

----------------------------------------------------------------
--------------------------------
----------------
--------
----
--
-
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++
++++++++++++++++
++++++++
++++
++
+

  Yet comment out that lone semicolon, and the results are very different.
In Lua 5.1:

lua-51: b.lua:12: ambiguous syntax (function call x new statement) near '('

But for versions of Lua 5.2 or higher:

----------------------------------------------------------------
--------------------------------
----------------
--------
----
--
-
function: 0x8aef2f8
lua-54: b.lua:2: attempt to call a nil value
stack traceback:
	b.lua:2: in main chunk
	[C]: in ?

  I was wondering what the issue was until I went back to Lua 5.1 and it
reported back an actionable error message (in my humble opinion) that
cleared the issue right up.  I'm just curious as to why the change?  Was it
harder to detect in Lua 5.2+?

  -spc