Difference between revisions of "Require"

From ComputerCraft
Jump to navigation Jump to search
m (Add external links)
m (Fix code formatting)
Line 9: Line 9:
 
| examples =  
 
| examples =  
 
     {{API doc example
 
     {{API doc example
     | name = Loading the '''cc.expect''' library
+
     | name = Loading the <code>cc.expect</code> library
     | description = Loads the built-in '''cc.expect''' module, and uses it within a function
+
     | description = Loads the built-in <code>cc.expect</code> module, and uses it within a function
 
     | code = local expect = require("cc.expect").expect
 
     | code = local expect = require("cc.expect").expect
 
local function check_string(x)
 
local function check_string(x)
Line 23: Line 23:
 
==External links==
 
==External links==
 
*[http://lua-users.org/wiki/ModulesTutorial Lua Module tutorial]
 
*[http://lua-users.org/wiki/ModulesTutorial Lua Module tutorial]
*[https://www.lua.org/manual/5.1/manual.html#pdf-require Lua manual for '''require''']
+
*[https://www.lua.org/manual/5.1/manual.html#pdf-require Lua manual for <code>require</code>]

Revision as of 14:55, 30 January 2020

require
Function
Syntax
require()
Returns anything
API shell
Source CC:Tweaked

Searches for a module on the current library path, returning the module or raising an error if it cannot be found.

ExampleExampleLoading the cc.expect library
Loads the built-in cc.expect module, and uses it within a function
Code
local expect = require("cc.expect").expect
local function check_string(x)
  expect(1, x, "string")
end

check_string(123)
Output
bad argument #1 to 'check_string' (expected string, got number)

External links