type

From ComputerCraft
Jump to: navigation, search

type
Function
Syntax
type(
  • variable : any
)

Returns string type
API Base globals
Source CC:Tweaked

Returns a string representing the type of the given variable.

ExampleExamplePrint various types
Prints the types of several variables
Code
local tbl = {}
local str = ""
local func = function() end
local bool = true
local number = 1
local nothing = nil
print(type(tbl))
print(type(str))
print(type(func))
print(type(bool))
print(type(number))
print(type(nothing))
Output
table
string
function
boolean
number
nil
ExampleExampleValidate an argument
Throws an error if the function is not called with the correct arguments
Code
local function foo(bar)
  if type(bar) ~= "number" then
    error("bad argument #1 (expected number, got " .. type(bar) .. ")", 2)
  end
end
foo(1)
foo("Hello World")
Output program.lua:7: bad argument #1 (expected number, got string)