Constant Variables
Constant variables are variables that are immutable after their first assignment, meaning they can never be changed. While they provide no performance benefit, as the compiler will propagate unchanged variables, constant variables are a safety feature for large projects where other developers might not realize that the author meant for the variable not to change. Here is a simple example of constant variables in action:
Constant Functions
Constant functions serve a similar purpose as constant variables do but have a slightly different syntax. If you have a function that performs a specific calculation and you don’t want to create a new one, you can do the following in Lua++:
With this syntax you are telling the compiler that you don’t need an entirely new environment and that the content of this function can be replaced with all the references in your code. Just like constant variables, the compiler can already identify whether functions like this are defined as constant or not, so from a performance standpoint, it doesn’t matter, but as a developer, this can make your code much cleaner and easier to understand.
Last updated