→Classes
(-external selflink) |
|||
| Line 20: | Line 20: | ||
=== Lua Namespaces === | === Lua Namespaces === | ||
In [[Lua]], namespaces don't directly exist. However, Lua programmers typically take advantage of several key, and unique, features which are available in the language to provide | In [[Lua]], namespaces don't directly exist. However, Lua programmers typically take advantage of several key, and unique, features which are available in the language to provide an indirect mechanism which can be considered setting up a namespace. Some other programmers, unfamiliar with languages that don't allow for the creation of namespaces, may confuse these with classes, but the major difference is that only one instance of a namespace ever exists, while there can be many separate instances of a class. | ||
In Lua, a function is a variable. As such, it should be noted that a reference to a function can be used wherever a variable is used. The implications of this is that functions can be stored inside tables. Furthermore, in WoW, all addons share the same [[execution environment]], therefore it is vital that some form of [[Lua_Scope|scope limitation]] is used so common variable names do not cause addon conflicts. Namespaces are one such method to organize code, thus limiting conflicts. | In Lua, a function is a variable. As such, it should be noted that a reference to a function can be used wherever a variable is used. The implications of this is that functions can be stored inside tables. Furthermore, in WoW, all addons share the same [[execution environment]], therefore it is vital that some form of [[Lua_Scope|scope limitation]] is used so common variable names do not cause addon conflicts. Namespaces are one such method to organize code, thus limiting conflicts. | ||
| Line 68: | Line 68: | ||
== Classes == | == Classes == | ||
Classes are very similar to namespaces, and implemented almost identically in Lua. Many programmers consider classes to be the fundamental concept of Object Oriented Programming, and the building block upon which other concepts can be created. They, like namespaces, are containers, but containers that can be duplicated. You can have many instances of a class, even though it's only declared once. Take the following C++ example: | Classes are very similar to namespaces, and are implemented almost identically in Lua. Many programmers consider classes to be the fundamental concept of Object Oriented Programming, and the building block upon which other concepts can be created. They, like namespaces, are containers, but containers that can be duplicated. You can have many instances of a class, even though it's only declared once. Take the following C++ example: | ||
class Foo | class Foo | ||