Unexpected Luau Functionality when Defining a Function as a Variable

Hi! I’m not sure if this is the right category, but -

Normally, in Lua, the two main ways of declaring functions,

function fn() -- Both local & global

end

f = function() -- both local & global

end

are syntactic sugar.

Because Luau is derived from Lua 5.1, in which this rule still applied, these should function the same way - whether it be debug.info(1, “n”), calling the function, and everything in between.

However, despite this:

Luau, at least Roblox’s branch of Luau, does not treat these two methods of declaring functions the same, particularly in debug.info().

local function f() 
	print(debug.info(1, "n"))
end

function g()
	print(debug.info(1, "n"))
end

local lf = function()
	print(debug.info(1, "n"))
end

lg = function()
	print(debug.info(1, "n"))
end

f() -- 'f'
g() -- 'g'
lf() -- '' (nil)
lg() -- '' (nil)

Output:

  00:29:04.952  f  -  Server - Script:2
  00:29:04.952  g  -  Server - Script:6
  00:29:04.952    -  Server - Script:10
  00:29:04.952    -  Server - Script:14


image

Expected behavior

However, if you look at Lua, in which I will just use an online compiler for ease of use (it remains functionally the same):

local function f() 
	print(debug.getinfo(1, "n").name) -- debug.getinfo() *should* work the same as debug.info in practice
end

function g()
	print(debug.getinfo(1, "n").name)
end

local lf = function()
	print(debug.getinfo(1, "n").name)
end

lg = function()
	print(debug.getinfo(1, "n").name)
end

f() -- 'f'
g() -- 'g'
lf() -- "lf"
lg() -- "lg"

If everything was working correctly, or at least in the same way Lua works, it should be printing these.

If any further information is needed, it will be provided if possible. Also happy to clarify anything because this was written at 01:17 at night, so my writing may not be the most clear.

A private message is associated with this bug report

3 Likes

Thank you for the report.

While Luau was based on Lua 5.1, it is a separate programming language and does not follow all Lua 5.1 behaviors.

In Luau, this is not a syntax sugar, but separately handled statements.

Luau also stores function names separately from variables that might hold a reference to that function, which is different from how Lua 5.1 tries to find an appropriate function name.

We do not plan to make changes here.

1 Like