This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
GamerNebulae's profile picture

When Is Something Nil And Null?

Started by GamerNebulae, 04 August 2013 - 09:19 AM
GamerNebulae #1
Posted 04 August 2013 - 11:19 AM
I have been talking to my dad and asked him what exactly nil was. He wasn't exactly sure and maybe you guys can answer me on that one. What exactly is the difference between nil and null and when is something nil or null?
theoriginalbit #2
Posted 04 August 2013 - 11:47 AM
a variable is nil when a value has not been assigned to it… nil is a keyword, just like null is a keyword in other languages, used to mean the same thing…

also as makerimages stated in Lua nil actually resolves to false, meaning that your if statements could be structured like so


if someVar then
 --# do something
end

instead of

if someVar ~= nil then
  --# do something
end
Lyqyd #3
Posted 04 August 2013 - 02:51 PM
The only distinctions between nil and null are their respective spellings and which languages they're used in.
immibis #4
Posted 04 August 2013 - 07:51 PM
In Lua, nonexistent table entries act as if they have the value nil. It's also the default value for global variables (which are really table entries) and local variables.

t = {test=123}
print(tostring(t["test"])) -- prints: 123
print(tostring(t["hello"])) -- prints: nil

print(tostring(asdf)) -- prints: nil

local x
print(tostring(x)) -- prints: nil