Type-Checking in LuaA recent discussion about using getters and setters in Lua led into a chat about the merits of type-checking. Lua does not provide the kind of type-safety you find in some other popular programming languages. In particular, Lua does not provide static type-checking, meaning you do not and cannot declare the types of variables like you can in Java or Haskell. That does not mean that Lua will not detect type errors, only that it will catch them later rather than sooner. Lua is similar to Python and Ruby in that regard. You can perform type-checking on your own in Lua via the
Not That OftenMy personal experience with Lua has shown that Type-checking for the purposes of sanity, i.e. making sure functions receive the correct types of arguments, has not been something I frequently need. It is true that this causes some type-related errors to slip through and not manifest until later on, where static typing would have caught the problem earlier. But again, clearly documented, concise and focused functions have helped keep these types of errors to a minimum. Where I Do Use Type-CheckingHere are the two most common scenarios where I find myself performing manual type-checking. Accepting Multiple TypesSometimes I write functions that accept an argument which may be one of many types. The code for LNVL has an example of this practice:
The L?VE function I’m calling can accept optional arguments. The code above reflects this and supports it by letting me use Note: The
Testing Object TypesLua supports object-oriented programming but it does not provide language features for declaring classes or objects like you will find in languages like C++ or Java. As you will see from that link, there are a variety of ways to approach OOP in Lua. But the overwhelming majority of developers use metatables as the foundation for their object-oriented code. I also use approaches where I use metatables to represent classes. So the second form of type-checking I use is to discriminate objects, i.e. checking the metatable (my classes) of tables (my objects). Here is an example, again from LNVL, of a function that changes the image of an object based on its class:
This is the most common from of type-checking that I use since it provides me with some measure of polymorphism. Object-oriented programming naturally encourages this style of type-checking since it is useful to group logically-related chunks of code which must behave slightly differently based on the objects they receive. Testing the value of metatables in Lua is the most common way to perform this type of discrimination. ConclusionAll that said, I still do not perform much type-checking in Lua. Even my two use-cases above are not the norm; they represent uncommon situations. When writing Lua code I try to keep in mind its weak typing compared to other popular programming languages. So instead of looking for ways to introduce the stricter type-checking of a language like C#, I instead ask myself, “What is the most simple and clear way to write this code without any concern for types?” Consistently asking myself that question along with documenting the answers has helped me avoid a lot of type-related errors in Lua. So I will end this article with some advice that I gave to another developer:
|
|