Introduction: The author who wrote this essay was motivated by a message in the email list of Lua saying “why isn’t Lua more widely used?” There are a variety of responses to this question. Some say it is for lack of class libraries, but the author supports another opinion more. That is, it is for the reason that most people do not like to solve problems by themselves. We always compare Lua with Arch Linux, which means that Lua is destined to be a product used by the minority. Here are some reasons for why I use Lua:
1. It can be integrated with C/C++
I do not have much experience of programming, but I have used a lot of languages, including Lisp，Scheme，Python，Perl，Bash，VBA，SQL, etc. None of them can work if you mix them with C. The API of C programming language is very clear, so it only takes you one afternoon to get familiar with it. If you are interested, you can have a look at “PIL’s section on the C API”.
Why is this feature so important? First, if you have a ready C/C++ class library, it is easy for you to use the API. Or you can write a foot script to let Lua execute in C language, which means the Lua program will not have the speed problem. If you think Lua is not fast enough, you can rewrite the codes in C language. Though programs of other languages can be rewritten in C language, I have not found anything more easily to achieve this than Lua.
2. It is fast and clear.
Compared to other languages, Lua is more efficient. It is probably because Lua uses the virtual machine that is based on a register, not a storehouse. Or we can say developers of Lua are all freaking awesome. Anyhow, you hardly need to take the performance of Lua codes into account.
But if you are still unsatisfied with the performance, you can try LuaJIT:
“LuaJIT is universally known as a dynamical programming language with the fastest performance. It occupies little RAM, with virtual machine occupying less than 125k and JIT compiler less than 85k (in the x86 environment).”
3. You can learn a lot.
Despite the fact that it is not a function of a programming language, I found that while using Lua, my knowledge about computer science has been increased. For example, I read the article about how Lua5.0 works and I learned that how the virtual machine works. My horizon was widened while reading something about source code. I think it all thanks to the steady improvement of Lua.
4. Functional Programming
Lua provides a framework of functional programming, which makes programming more fun. The features such as anonymous functions, higher order functions, lexical closures, proper tail calls are not syntactic sugar of some languages. However, Lua integrated the features at the bottom level. For instance, every method of Lua is a value (such as number 2, or a character string “ktr”), which means that every method is anonymous. You just need to give them names so that you can use them. You can reassign print:
print = math.sin
You can use this function to create a sandboxie. If the print is a dangerous process, you can replace it with your own print or cancel it.
5. Everything is a table
In Lua, every variable type is a table, such as Array，Hash，“module” or global variables and so on. It means you can “require” some modules (resembles Python’s import sentences), and regard results as tables. In Python, you can__import__(‘…’) some modules, and the variables of the return value can be visited by a method resembling var(module. In Lua, you can visit variables of modules through module[var]. Mentioning Python, I meant to emphasize the difference and convenience of Lua, and it will also help you learn the grammar of Lua faster. You do not have to worry about the difference between list and dictionary or package.
6. The consistency
I do not know if you have realized that, Lua is a pretty consistent language. A lot of functions of it were made just right. For example, Lua does not have the FOR-Loop until Lua 4.0, 7 years later after Lua was released. That is because the WHILE-Loop is more popular, and the grammar is more concise. The development team of Lua is comparatively conservative, and they cannot accept patch, so the evolution of Lua is slow but steady. You know, every function is under planning, which means you cannot expect your expecting functions can be realized very soon.
I will not talk about a lot of details about this point, but Lua can be operated in almost all environments. Codes of Lua are compiled according to the “Clean ANSI C” standard. So if you have a C language operating environment, you can also use Lua.
In a conclusion, Lua is a very simple but also a very powerful language.
Did you like this? Share it: