lua-users home
lua-l archive

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

On Wed, 22 Jul 2009 12:14:04 -0700
Wesley Smith <> wrote:

> After reading module v. dofile discussion I was curious how people
> handle OO constructs as Lua modules.  I like having the module itself
> act as the "class" or "prototype" and use it as a constructor for an
> instance of that module or class.  

I use the second argument to the module function to specify the
superclass from which the class in a module is to be derived:

local polygon = require "polygon"
module("rectangle", polygon)

I don't know if somebody else came up with this idea, but I find this
rather nifty. At first I thought inheritance in Lua had to be confusing
and impracticable for large projects, but with this approach it all
became clearly organized and sober. (My base class also bails out with
an error message when uninitialized ('nil') members in a class are read
or written to - this also improved OO a lot for me. In Lua you can
fabricate tools that really suit your needs.)

- Timm

Timm S. Mueller <>
Schulze & Mueller GbR, Gryphiusstr. 2, 10245 Berlin,
Gesellschafter: Franciska Schulze, Timm S. Mueller,
Tel. +49 30 93624410,