[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: OO - keeping lua as lua
- From: Adrian Sietsma <adrian_groups@...>
- Date: Tue, 25 Jan 2005 18:07:09 +1000
There has been a lot of mail on this list re Lua and Object Oriented
Programming, functional programming, etc.
I have some views (who hasn't) on this.
1/ Lua already supports (mimics?) OOP via the metatable / proxy table
2/ A lot of OOP is about encapsulationg, hiding and protecting data,
methods, etc., as well as the inheritance issues. This is all very well,
but Lua allows me to do things that are anathema to OOP, particularly
adding new members to an existing object.
t = newobject(...)
t.x_descript = "zorro" -- add member var to object
f = io.open(file)
getmetatable(f).newfunc = new_metafunc -- add class function
new_open = function(...) return io_open(...) end
io.open,io_open = new_open, io.open -- re-vector method
do i want users of my C++ libraries to do that ? no.
do i want to do that to other C++ libraries ? no. (not often)
do i want users of my Lua libraries to do that ? why ever not!
do i want to do that to other libraries ? of course!
apart from anything else, it makes library extensions easier. i use an
extended os.lib - would i rather type os.dir() or os_aux.dir() ?
also - if i were to write LuaSound, fred (i don't think there are any
freds on the list) could write a whole heap of nifty add-ons, and build
an extended lib, without my involvement. which is good.
none of this says that you can't make un-modifiable objects, classes,
etc : but it would be wrong for this to be the default.
might i suggest that a lua.class library could be an interesting way to
encapsulate a pure OO syntax. this would also be a lua-ish way to do
things, without changing the core.
(a class being a userdata may be interesting : store per-object data in
the metatable, and use the __index metafunc to give
obj.method() and obj.attribute syntax, if you like)