Barrack - A simple type checking library
Posted: Wed Jul 20, 2016 3:22 am
This library is meant to make it easier to track down errors when a variable of the wrong type is passed to a function.
When enabled, barrack will check the arguments of a function against a list of definitions to ensure that invalid arguments do not get passed.
If an argument fails the definition test, barrack will raise a verbose error explaining what type of variable was expected, and what was received instead.
Example:
ToDo: Allow multiple definitions for a single argument. For example:
In this case, 'x' and 'y' must be numbers. But 'info' can be either a table or a string.
https://github.com/danielpower/barrack
When enabled, barrack will check the arguments of a function against a list of definitions to ensure that invalid arguments do not get passed.
If an argument fails the definition test, barrack will raise a verbose error explaining what type of variable was expected, and what was received instead.
Example:
Code: Select all
barrack = require('barrack')
-- Barrack is disabled by default. barrack.check is a blank function unless enabled.
-- This is to remove any performance hit in production, as barrack is mainly meant for debugging.
-- You can disable barrack with barrack.disable().
barrack.enable()
function createInstance(x, y, name, info)
barrack.check( { x, y, name, info },
{ 'number', 'number', 'string', 'table' })
-- Continue writing the rest of your function as you normally would.
-- Barrack will raise an error if any of the arguments do not match the required type.
end
Code: Select all
function doSomething(x, y, info)
barrack.check({x, y, info}, {'number', 'number', {'table', 'string'})
-- Do things
end
https://github.com/danielpower/barrack