So with all that said, this is me "showing you up" with no explination.
local verColor = (term.isColor and term.isColor())
if verColor == true then term.setTextColor(colors.black) term.setBackgroundColor(colors.lightBlue) end
print("Code/Text of Program")
I'm not sure how that's "showing me up," whatever that means, because it's just my code inlined (taken out of a function and inserted directly, in place of the function call)
I'm still learning here, but, i thought "and" could only be used with things like "if".
(To clarify, i have no idea what ternary operator is and this is more of a question.)
Okay, this is how it works.
In Lua, everything that is not false or nil is considered true.
Note that nil is not equal to false, even though they act the same way in an if statement.
What "X and Y" actually does is it returns X, if X is false or nil. Otherwise, it returns Y.
If X is false or nil, it does not attempt to evaluate Y, so nothing will happen if Y would have raised an error.
So
return term.isColor and term.isColor()
can be rewritten as:
if term.isColor == false or term.isColor == nil then -- this line is equivalent to "if not term.isColor then"
return term.isColor
else
return term.isColor()
end
The default value of variables and table fields in Lua is nil, so on CC versions without colours, term.isColor is nil, and the function returns nil.
This is mostly equivalent to returning false, unless you test for false specifically, eg:
function advIsColor()
return term.isColor and term.isColor()
end
-- This won't work if term.isColor doesn't exist, since advIsColor will return nil, which is not equal to false.
if advIsColor() == false then print("Not colour") end
-- All of these will work
if advIsColor() ~= true then print("Not colour") end
if advIsColor() then print("Colour") end
if not advIsColor() then print("Not colour") end
Your version returns false if term.isColor doesn't exist, and mine returns nil. If you wanted to modify mine to return only true or false, either of these work:
function advIsColor()
return (term.isColor or false) and term.isColor()
end
function advIsColor()
return (term.isColor and term.isColor()) or false
end
These work because "or" does the opposite of "and".
In "X or Y", X is returned if it is
not nil or false, otherwise Y is returned.
The first one is equivalent to:
function advIsColor()
local temp
-- the "or" part. I save the result in temp to save repeating this part twice.
if term.isColor ~= false and term.isColor ~= nil then -- again, this can be abbreviated as just "if term.isColor then"
temp = term.isColor
else
temp = false
end
-- Now temp is either false, or term.isColor
-- the "and" part
if temp == false or temp == nil then
-- Temp can't be nil, because it was either false or term.isColor, so it must be false
return temp
else
return term.isColor()
end
end
Edit: The "ternary operator" is something that Lua doesn't have, but it's found in other languages like Java and C. In these languages, instead of writing, for example:
if X then
return Y
else
return Z
end
you can write:
return (X ? Y : Z)
as a shortcut.
"(X and Y) or Z" does the same thing in Lua as long as Y is not nil or false.