Is there a const keyword in lua ? Or any other similar thing? Because i want to define my variables as const and prevent change of the value of the variables. Thanks in advance.
The const keyword is used to create constants.
This means that all values can be stored in variables, passed as arguments to other functions, and returned as results. There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.
We use the const qualifier to declare a variable as constant. That means that we cannot change the value once the variable has been initialized. Using const has a very big benefit. For example, if you have a constant value like the value of PI, you wouldn't like any part of the program to modify that value.
Description. The const keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable "read-only".
I know this question is seven years old, but Lua 5.4 finally brings const
to the developers!
local a <const> = 42 a = 100500
Will produce an error:
lua: tmp.lua:2: attempt to assign to const variable 'a'
Docs: https://www.lua.org/manual/5.4/manual.html#3.3.7.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With