Toggle menu
15
248
116
28.1K
Kenshi Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:CallAssert/doc: Difference between revisions

From Kenshi Wiki
Prd (talk | contribs)
Created page with "{{Module rating|r}} {{used in system}} This metamodule provides function '''callAssert''', which simplifies error checking by throwing an error if a given function returns nil or false. This is similar to the built-in function assert, but unlike it, callAssert itself calls the function, whose return value is to be checked. This allows it to include the function name and argument values in the error message. == Usage == loc..."
 
Prd (talk | contribs)
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Module rating|r}}
{{doc sub}}
{{used in system}}
This [[Help:Lua metamodules|metamodule]] provides function '''callAssert''', which simplifies error checking by throwing an error if a given function returns nil or false. This is similar to the built-in function [[mw:LUAREF#assert|assert]], but unlike it, callAssert itself calls the function, whose return value is to be checked. This allows it to include the function name and argument values in the error message.
This [[Help:Lua metamodules|metamodule]] provides function '''callAssert''', which simplifies error checking by throwing an error if a given function returns nil or false. This is similar to the built-in function [[mw:LUAREF#assert|assert]], but unlike it, callAssert itself calls the function, whose return value is to be checked. This allows it to include the function name and argument values in the error message.


Line 10: Line 9:
<code>callAssert</code> calls <code>''func''(...)</code> and checks if the first returned value evaluates to true. If it does, then it returns all the returned values. If not, it throws an error with a message in the form <code>{{'}}''funcName''(''argument_values'') failed'</code>.
<code>callAssert</code> calls <code>''func''(...)</code> and checks if the first returned value evaluates to true. If it does, then it returns all the returned values. If not, it throws an error with a message in the form <code>{{'}}''funcName''(''argument_values'') failed'</code>.
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox | |
<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox | |
[[Category:Modules]]
[[Category:Error-handling modules]]
[[Category:Error-handling modules]]
}}</includeonly>
}}</includeonly>
<noinclude>
<noinclude>
[[Category:Module documentation pages]]
[[Category:Module documents]]
</noinclude>
</noinclude>

Latest revision as of 23:58, 10 March 2025

This metamodule provides function callAssert, which simplifies error checking by throwing an error if a given function returns nil or false. This is similar to the built-in function assert, but unlike it, callAssert itself calls the function, whose return value is to be checked. This allows it to include the function name and argument values in the error message.

Usage

local callAssert = require('Module:CallAssert')
local result1, result2, etc = callAssert(func, 'funcName', ...)

callAssert calls func(...) and checks if the first returned value evaluates to true. If it does, then it returns all the returned values. If not, it throws an error with a message in the form 'funcName(argument_values) failed'.

Contents