Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any keyword like const or anything else which does the same job with it in lua?

Tags:

constants

lua

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.

like image 848
oiyio Avatar asked Sep 11 '12 11:09

oiyio


People also ask

Which keyword is used to make constant?

The const keyword is used to create constants.

How many functions does Lua have?

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.

What is the use of const keyword explain with an example?

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.

What keyword means constant?

Description. The const keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable "read-only".


1 Answers

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.

like image 98
Ainar-G Avatar answered Sep 20 '22 21:09

Ainar-G