lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Mark Meijer wrote:
2008/9/13 Veli-Pekka Tätilä <vtatila@gmail.com>:
> [...]
> module( ..., package.seeall )
>
> The way I've understood it this will:
> * name and create your global module table (aka. class) after the
> physical file basename
> * make sure that "global" assignments incl. function  definitions go in
> your module table
> * fetch stuff from _G if it is not found in your module, when looking
> for "globals"
>
> The only disadvantage I've found is that I can no longer use the colon
> syntax. I would say:
>
> function class:setX(x)
>
> But now, it becomes:
>
> function setX(self, x)
<snipping myself>
This is based on the assumption that a module equals a class. I don't
think modules were necessarily meant to be used in that way.
Personally, I tend to view the module name as something like a
namespace. The namespace may contain just functions or entire classes,
depending on the design style for that particular project. But a
namespace containing functions (i.e. a module exposing functions) does
not necessarily equal a class, in my view. Hope I'm making sense.
You are, I mostly agree. Take the math library for example, it is just a bunch of related functions grouped under a common name so as not to clash with other names. Or the IO library where, again, we have a collection of functions for stdin and stdout that just happen to also work as methods if you create a file handle to call the functions against. Similar syntax sugar is there for strings due to the metatable concept. It reminds me of Perl packages trying to support both OOP and procedural use.

Whether a module with maybe some data fields and a bunch of functions is a class is a matter of terminology. IF the language forces me to call it such syntactically, e.g. Java, I call it one. If not, say for Lua and Perl, I tend to call the conventional OOP stuff classes and the rest with the native name of such a construct i.e. modules in Lua. Somewhat inconsistant, I know, but calling everything a module tends to confuse people who don't know Lua 5.1 modules.

--
With kind regards Veli-Pekka Tätilä
Accessibility, Apps and Coding plus Synths and Music:
http://vtatila.kapsi.fi