In Search of Class

Lua is a scripting language. It is one of the options for scripting in Verge, and is also a language that I’m using in another project that I’ll be working on in the next while. It’s fast and easy to embed into other applications. It’s also powerful and svelte, thanks to some neat language tricks and the way it is built.

One issue with Lua, though, is that it lacks some pretty core Object Oriented concepts. You don’t have classes or objects; the closest that you get are a data type called tables. Basically, tables are just a hashed associative array. Like all Lua types, tables also have a metatable which is another table with code dictating how things like looking something up works. On top of this, you get syntactic sugar like the fact that tbl[‘key’] is identical to tbl.key, and tbl:func(a,b,c) is the same as calling tbl.func(tbl,a,b,c).

With all of this you can easily replicate one way to do object orientation, which I call the “clone object paradigm”. Basically, you can create an object, and then clone that object as much as you want. The clones are identical copies of the original object until you change something about them, at which point they differ only on that one change.

I’m not a huge fan of this paradigm. It feels kind of kludgy and a little difficult to keep track of things that are supposed to be “blueprints” and things that are actually objects that you want to be working with. Currently though, it’s how both Geas and my other project do things.

What I set out to do (and have subsequently put a fair bit of time into) is figuring out a way to replicate a more C++ (et. al) style: the “class/object paradigm”. In this object orientation style, you have classes and you have objects which are instantiations of those objects. Because of this enforced difference, you have a clearer divide between the things you should be working on and the things you shouldn’t be.

Unfortunately there are a lot of nasty problems that crop up trying to do this. The metatable references and figuring out exactly what should be setting, calling, or reading what has proven to be a lot hairier than I had hoped it would be.

So, that is why for my Gruedorf update this week all I have is a rambley, poorly written explanation of object orientation. My lua code does some interesting things but is not in a state that is useful in any way. Yet. I haven’t given up, but I will be pushing this onto a back-burner as far as my Gruedorf posts are concerned. I don’t want to waste any more weekly posts on this, so unless I get it working behind the scenes and then spend a week refactoring my vklua library to use the new scheme, you won’t hear about it again.

I hear McGrue has done something cool this week. I don’t know what it is yet, but I’m sure there will be something over there to read sometime this evening. Hopefully he’ll make up for me sucking this week.



Overkill ( , ), 2008-10-05 23:04:50

Well, one thing you can do if you don’t like the “cloning” method, is to use LuaBind’s class system (which is what LuaVerge is embedded with). It’s what both me and ustor have used.

class "MyParent"

function MyParent:__init() = 5

function MyParent:DoSomething()

class "MyClass"(MyParent)

function MyClass:__init()
    super() = 124

function MyClass:DoSomething()
    print("Wow this is cool")

myvar = MyClass()

The only lacking is no way of adding Properties, so I actually made a “vergeclass” function in vx just to put that in. I also made a “namespace” system to require a bunch of external file’s classes under one file, works fairly good.

Lua’s full of hacky table mashed togetherness, but it makes it easy to implement your own custom class systems. It feels wrong when you write it, but it works WELL when you use this encapsulated piece of hackiness somewhere else.

Also why aren’t you using vx? It’s fairly nice to use! And it’s documented!

Re: In Search of Class

Overkill ( , ), 2008-10-05 23:04:50

Also, you need to get it so that people can actually read comments without logging in (and show a comment count)! It’d be nice! I mean, logging in makes sense for POSTING comments, but VIEWING? Come on now.