-
Notifications
You must be signed in to change notification settings - Fork 1
Variables and Constants
Variables and constants work just as they do in regular Lua; but unlike regular Lua, LuaScript variables are hard typed.1
So when you declare a variable in LuaScript you need to indicate the type. If you don’t, the type of the variable will be set to the type of the value you first assign it.
You can op-out of the type verification system by declaring a variable with the type any.
These are extracted via the type function.
Lua has 6 types: string, number, boolean, table, function and userdata.
LuaScript adds 5 more: enum, prototype, class, object and any.
-
stringare string values like"this is a string",'this is also a string'or[[another string]]. -
numberis for numeric values like144,math.pior0xA9. -
booleancan either betrueorfalse. -
tableare regular Lua tables. -
userdataare a raw memory area used in Lua. -
functionare regular Lua functions.
For ease of use, LuaScript has implemented a rudimentary type casting system.
- Placing a
$in front of a variable transforms it into a string$variableName = tostring(variableName). - Placing a
#in front of a variable transforms it into a number#someVariable = tonumber(someVariable)(placing a#in front of a variable pointing to a table instead calls it’s__lenmetamethod).
LuaScript also has a simple null propagation method.
When calling a value inside a table, if you place a question mark ? after the variable name you can catch possible errors for attempting to index nil values.
local tableZ = { variableA = { variableB = 102 } }if (tableZ.variableY.variableB > 100) then ... // the compiler should throw an error since you're trying to index a nil value if (tableZ.variableY?.variableB == 102) then ... // there is no error here, it just returns false as nil is not equal to 102
It also works with functions.
local tableZ = { functionB = function(self, v) print(v) end }tableZ.functionC("foo") // again, the compiler should throw an error since you keep trying to index a nil value tableZ.functionA?("bar") // but there is no error here, simply nothing happens
The var keyword declares new variables and the const keyword declares new constants.
... {
const test1 = false // constant
var test2: string = "hello world!" // variable, string
var test3 = 144 // variable, number
}You can declare the value of a variable or you can initiate it with a generic value placing a ? after the type.
Generic init values are "" for strings and any, 0 for numbers, false for booleans, {} for tables, function() end for functions, and an empty enum, prototype, class or object for such types.
... {
var test1: table = {1, 3, 14} // declared with value
var test2: string? // declared without value
}Variable declarations of the same kind can be clustered together.
... {
var layer: any?, width: number?, height: number?, backColor: table?
const screenw = 320, screenh = 200
}You can also force a value into a variable by including an exclamation mark ! at the end of the expression (or using the rawvar function).
... { var test1: string?, test2 = 144, test3: boolean?func testFunc() { self.test1 = math.pi // error: String expected, got number (var: test1) self.test2 = rawvar("some string") // test2 now has a string value self.test3 = {1, 3, 4}! // test3 now has a table value } }
Careful when using rawvar since it only forces the value into a variable, it does not change it’s type.
1 The following only applies to variables and constants declared inside prototypes and classes.
LuaScript Docs
Repository · Documentation · Releases
Documentation module for the LuaScript language.