I saw this piece of code:
if true then
return
end
What does it return in this case? Is the piece of code above equivalent to this:
if true then
return nil
end
I saw this piece of code:
if true then
return
end
What does it return in this case? Is the piece of code above equivalent to this:
if true then
return nil
end
This code returns ’ ’ and not nil.
nil
is the representation of a non-value at all, but if I recall correctly it doesn’t return nil
(the first code), internally rather it returns void
(which is nothing). I’m not sure if I’m correct, so if anyoneone know, please correct me.
Yes, it returns void.
function f() if true then return nil end end print(f())
function f() if true then return end end print(f())
So, if I return void, then nothing will be shown in the output?
As simple as it sounds, yes. That’s the difference.
correct, but they’re also comparable
print(f() == nil) -- f() returns void, the output was: true
print(not f()) -- also does the same thing as above
Alright! Now I understand this! Thanks a lot for your replies!
A return
statement with nothing after it will return nothing, nil
.
return
and return nil
are essentially the same.
Both versions return nil, return
on its own will not return an empty string value.
local function test(arg)
return arg
end
print(type(test(nil))) --nil
print(type(test())) --nil
I just tried to say what it is going to show. No string.
In addition to the above, this can be used to stop scripts
print("A")
if true then
return
end
print("B")
will never be printed B, is an alternative to avoid turning the rest of the script into comments.
void
isn’t an existing DataType in Lua, it’s just a way of describing a function/method which doesn’t return a value.
Yes, in Luau (nor Lua) the void
data-type exists, I meant internally it is like that, if you print the type of the return from f()
function call, you’ll obviously get nil
as the result because there’s no way to tell it is void
.
local function isVoid(f, ...)
local v = f(...)
if type(v) == "nil" then
print("Function is void.")
else
print("Function isn't void.")
end
end
local function void()
return
end
local function notVoid()
return true
end
isVoid(void) --Function is void.
isVoid(notVoid) --Function isn't void.
return arg
means it’ll always return one value regardless whether it’s f()
or f(x)
. return
means it returns no value.
Where on earth did you get the fact that
static int func (lua_State *L) {
return 0;
}
and
static int func (lua_State *L) {
lua_pushnil();
return 1;
}
are essentially the same?
A counterexample:
local function a()
return
end
local function b()
return nil
end
print(select("#", a())) --> 0
print(select("#", b())) --> 1
For the compiler I meant, you can as well make an overload by yourself and detect it without a problem, but for Luau/Lua is impossible, since it doesn’t know what is void
itself.
nil on lua means nothingness or non-existance. so if there is something that has an obejct value. for example: A surfaceGui. it has adornee property. adornee property is an instance. so if there was no adornee then adornee is nil.
also, for example an instance with no part is deleted. so the parent of it is nil.
ObjectValues was just an example
but nil is like nothing
also datastore is another example.
lets say you do:
DataStore:SetAsync(Blabla, Value)
Now if you do DataSotre:GetAsync(Blabla) it returns Value,
but if the datastore did have the Blabla for example:
You never said DataStore:SetAsync(Blabla, Anything)
then when using DataStore:GetAsync(Blabla) it returns nil
It is possible in Lua(u) though. (Also there’s no void
data type in Lua(u) internally - it doesn’t push any value to the stack)