type

Returns a string representing the type of the given variable.

ExamplePrint various types
Prints the types of several variables
Code
local tbl = {}
local str = ""
local func = function() end
local thread = coroutine.create( function() end )
local bool = true
local number = 1
local nothing = nil
print(type(tbl))
print(type(str))
print(type(func))
print(type(thread))
print(type(bool))
print(type(number))
print(type(nothing))
Output
table
string
function
thread
boolean
number
nil
ExampleValidate 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)

type
Function
Syntax
type(
  • variable : any
)

Returns string type
API Base globals
Source CC:Tweaked
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.