Module:Constructor/doc

This module is a module used to convert types into other types with metamethod support for the module's methods.

Loading the module
To load this module and make its methods available for use, Add this line of code to the start of your module:

This module does not need to be loaded under a table to be used. Once the module is loaded, any methods described below will be available as global variables in the loaded module.

Alternatively, you can use this module using the code below by using Module:LoadLib. Note that if you use this function, you do not need to add the module to the loader query because this module is implicitly loaded by the function.

Depending on the settings you used in, the methods of this module may be available under their respective variables in the module this module was loaded in.

Methods
All methods are available below as fields under the required object name.

String
This Method returns converted to a string value when it is called. It will respect any metamethods and will use that metamethod before trying to convert is value to a string (see  for an in-depth metamethod explanation).
 * This method Also has contains all methods from Module:String as fields on this method. (this includes native functions like ).

Examples

 * String({ "foo" }) → "table"
 * String(true) → "true"
 * String → ""

Number
This Method returns converted to a number value when the method is called. It will respect any metamethods and will use that metamethod before trying to convert is value to a string (see  for an in-depth metamethod explanation).
 * This method also has contains all methods from the lua math library as fields on this class. (for example, ).
 * If is a string or table, it will return the length of that value (for a table, it will use.

Examples

 * Number("FooBar") → 6
 * Number(true) → 1
 * Number → 0
 * Number(function end) → NaN

Table
This Class returns each value of packed into a table similar to. If the length of the arguments is less than 2, and the value has a __totable metafield, it will use that metafield on the table (see for an in-depth metamethod explanation).
 * This method Also has all methods from Module:Table as fields on this class. (this includes native functions like ).

Examples

 * Table("foo", "bar") → { "foo", "bar" }
 * Table(true) → { true }
 * Table → {}

Boolean
This Class returns converted to a boolean value when it's constructor is called. It will respect any metamethods and will use that metamethod before trying to convert is value to a string (see  for an in-depth metamethod explanation).
 * This method also contains a method called which determines if the value is boolean.
 * This method will return false if is any of the following:
 * nil
 * false
 * 0
 * NaN
 * any variant/alias of the string "false", ex: "f" → false
 * Else, it will return true.
 * Else, it will return true.

Examples

 * Boolean("foo") → true
 * Boolean(true) → true
 * Boolean(0) → false
 * Boolean(1) → true
 * Boolean → false

Function
This Method cannot be called as a function, but has some useful methods. The following consists of the method's methods.

Function.makeMethod
This method creates a method with if it is a function. If is a table, it iterates over the table where each key is a method to be made into  the type set by. The returned value (the return value is a table with each value as a static method if the passed value was a table of methods) is meant to be used in.

Metamethods
Each method listed here has a corresponding metamethod which is used when it's respective function is used and the value being passed to the function has a metatable to call the metamethod from.
 * Note that uses the native  metamethod. It's documentation can be found here.
 * Also note that, , and are the same in how they operate when used.
 * Also note that does not have a metamethod due to MediaWiki modules not having access to.

The following list shows which metamethod corresponds to which method listed here:
 * __totable Corresponds to.
 * __tostring Corresponds to.
 * __tonumber Corresponds to.
 * __toboolean Corresponds to.

__totable
If the metamethod is a function, it is called with the following parameters:
 * is the table the method was called on.
 * is a table containing the keys and values of the table the method was called on.
 * is a iterator for use in a for... ...in... ...do... ...end loop.
 * You can simply pass this variable inside the function to the loop to start iterating. Note that this iterator has already had a value passed into it so there is no need to call it when using.
 * Is the total length of the table. This includes named parameters.

If the metamethod is not a function, it uses the value of the metamethod.

__tostring/__tonumber/__toboolean
// If the metamethod is a function, it is called with the following parameters:
 * is the value the method was called on.

If the metamethod is not a function, it uses the value of the metamethod.