Quirrel4.41.0

types.Integer.constructor

Method on every integer
constructor(...): any

Converts value to an integer. This is what runs when types.Integer itself is called, types.Integer(value).

Return value

0 when called with no arguments. Otherwise value converted to an integer: an integer is returned as is, a float is truncated toward zero the same as tointeger, a bool gives 0 or 1, and a string is parsed as a number in the given base (default 10, so types.Integer("2A", 16) reads "2A" in base 16).

Errors

Throws cannot convert to Integer for a table, array, null or any other type a number cannot come from, and cannot convert string to Integer when the string does not parse in the given base.

Notes

Takes zero to three arguments: value and base are both optional, but the VM cannot show them by name here - this binding carries no declaration string, so it dumps as the generic fallback (table|userdata|instance|class|null).constructor(...), the same placeholder receiver described on types.String.constructor. Calling it with four or more arguments does not throw either; the extra ones are read but never used.

constructor is a reserved word, but the parser special-cases it after a dot, so x.constructor parses like an ordinary field access - unlike clone, which does not get that exception.

Example

examples/types/integer/constructor.nut
let types = require("types")
println("types.Integer() =", types.Integer())          // no args: 0
println("types.Integer(3.9) =", types.Integer(3.9))       // truncates toward zero, like tointeger()
println("types.Integer(\"2A\", 16) =", types.Integer("2A", 16))  // string parse with an explicit base
println("types.Integer(true) =", types.Integer(true))      // bool -> 1
try { types.Integer({}) }
catch (e) println("types.Integer({}) throws:", e)
Output:
types.Integer() = 0
types.Integer(3.9) = 3
types.Integer("2A", 16) = 42
types.Integer(true) = 1
types.Integer({}) throws: cannot convert to Integer

See also

tointegerReturns the integer itself.
constructorConverts value to a float. This is what runs when types.Float itself is called, types.Float(value).
constructorConverts value to a bool. This is what runs when types.Bool itself is called, types.Bool(value).
classofReturns the class that obj belongs to.
integerclass index