No, it is not the same. This is because of the lexical scoping property of functions, the fact function
is a constructor - not variable declaration.
local function a()
print(a)
end
interpreter goes “alright, its a function constructor, which means we’re making a function value, let me index the code, which has a inside it, and now that I’ve done that i’ll assign the new function value to a new variable a” – in that order. When you run the function, its index already has a in it, thus its an upvalue.
going
local a = function(...)
print(a)
end
interpreter goes “alright, a is being declared in this top scope, and it looks like to a function constructor, which just returns a function value, so i’ll index this function, and return/assign it back to a
.” thus, a
is in the scope above when the function is indexed, meaning its a global.
the moment you do local a
is the moment you’ve made your variable in memory, where the that isn’t the case with local function a()
because functions have their own constructor, like lua tables have their own constructor, thus construction comes first.
if this was not the case, then we couldn’t have anonymous functions (functions without variables, aka local function(...) end
.
I will give you points though because the original Lua manual incorrectly calls this “syntatic sugar”, as if to imply they’re the same, despite disproving itself in the later paragraph by talking about how function declarations are a constructor. You could call this an oversight, but it serves an actual functionality that is distinct.
As for the below:
local a = part.Touched:Connect(function(p)
a:Disconnect() --error
end)
“create a, assign it to… RBXScriptSignal… which is whats returned from Connect, which indexs the function value given to its parameter, all before a is assigned to, even though a exists, its just blank.” thus you cannot disconnect. I admit this one is an annoying symptom of it and I don’t really care to defend it.
to add on, also, if we didn’t do it like this, then you can’t really do local a: any
because assignment would have to occur to initialize the variable