Please note: this page is now obsolete. This semi-joke version has been replaced by a shiny, new, and entirely real version of Objective Lua, now hosted on SourceForge.

Objective Lua

Everything following is kept for historical reasons and refers only to the toy version --- which you can still get, if you want.

So, what is this?

Objective Lua is a new programming language that consists of the object-oriented system from Objective C welded onto Lua.

Are you serious?

Actually, no. Instead, Lua provides all the low-level tools so that you can trivially create you own object-oriented system in only a few lines of code.

If I remember correctly, Objective C uses multipart keyworded method names, where a call looks something like [object moveByX: 4 Y: 3]. How have you adapted that to Lua syntax?

I don't. You use all the traditional Objective C syntax, in Lua.

How does that work, then?

Objective Lua programs are converted into standard Lua using a translator, which is currently a very small Leg plugin.

I'm appalled, but... intrigued. How much Objective C syntax does this abomination support?

Objective Lua supports @interface, @implementation, method calls with [] notation, superclass method calls with [super], and both normal and class method types. Here's an example:

@interface Point : Object
@end

@implementation Point
- init
[super init]
self.x = 0
self.y = 0
return self
end

- initX: x Y: y
self.x = x
self.y = y
return self
end

- x
return self.x
end

- y
return self.y
end

- moveToX: x Y: y
self.x = x
self.y = y
end

- moveByX: x Y: y
self.x = self.x + x
self.y = self.y + y
end
@end

local point = [[Point alloc] init]
[point moveToX: 4 Y: 9]


Wow. That looks... pretty clean, actually.

Yes. To my total surprise, it was a really good fit. Lua's dynamism suits the Smalltalk-descended architecture of Objective C beautifully, while the formalised class structure of Objective C gives order and shape to Lua's otherwise very free-form programs.

I notice you're not declaring your methods or your properties in the @interface block.

Yes. Predeclaring methods isn't really applicable to a dynamic language like Lua --- we can add methods on the fly at any point. In fact, the only thing the @interface block is doing right now is specifying the superclass. Future versions will probably change the syntax to make it more Lua-friendly, but right now I wanted to keep as traditional Objective C syntax as possible.

What's the performance like?

Under the hood it turns into normal Lua. This:

local point = [[Point alloc] init]

...becomes:

local point = Point:alloc():init()

So the performance is identical to any other table-based object system.

In fact, one of the advantages of using Objective Lua over traditional Lua is that the object system becomes divorced from the actual code. It would, for example, be possible to switch to using a closure-based object system for extra speed, without needing to change any application code.

Huh.

Yeah.

Does it bridge to Objective C?

Not currently. However, with projects like LuaObjC, it should be very easy. Because Objective Lua just does standard Lua method calls behind the scenes, interoperability ought to be trivial.

Okay, I'm sold. I'm going to write all my code in Objective Lua from now on.

I'd hold off for a while yet. The current Objective Lua compiler is a toy --- it's only 200 lines long! It's intended to provide a working example and talking point only. For example, there's one nasty grammatical edge case if you follow a statement that might be an expression with a method call:

someFunction() [object someMethod]

Leg right now gets confused because it things the [ is starting an array subscript. In fact, this is not ambiguous, because a method send can always be distinguished from a subscript by the existence of the method selector, but you may need quite a lot of readahead to do it (as this appears after the expression denoting the receiver). So Objective Lua won't become truly useful until someone rewrites the compiler to get all this stuff right.

(This, by the way, is why the test program occasionally parenthesises method calls, or puts a ; after a statement --- to eliminate ambiguity to give Leg a leg up.)

Besides, right now it gets all the line numbers wrong, which makes programs a pig to debug.

So what use is it?

It's a toy.

Smalltalk was one of the earliest object oriented languages around, and still one of the best. Objective C took a very pragmatic approach to extending C with an object-oriented system by simply stealing the Smalltalk syntax and semantics wholesale. I was curious to see how well the same principle would apply to a different language. So far, I'm pleasantly suprised --- even writing the noddy test programs that I've done so far seems to be a fundamentally cleaner experience than writing the same code in raw Lua.

I don't imagine that anyone will ever want to turn this into a real product, but I've been wrong before (frequently). At the very least, I'm hoping that it'll provoke discussion.

I recently had reason to take another look at Smalltalk and a very clever strongly-typed but compatible derivative, Strongtalk, and was reminded again of the sheer elegance of the language. I wondered if it might be possible to build a Smalltalk to Lua translator, and then wondered about implementing the Smalltalk object system in Lua... and then I thought of this.

What, really?

Plus, I was bored.

I want to send you a detailed critique.

My email address is at the bottom of the page, under the comment form (hint, hint).

I want to help.

Feel free to send me patches. Feel even more free to send me a clean recursive-descent parser for Lua 5.1 that will generate an AST, and then serialise the AST back out to Lua source code (getting all the line numbers right)...

I think you're a genius and want to have your babies.

Thank-you. Please email me proof of XX chromosomes and a photo.

I think you're evil and will go to hell.

See previous question.

Well, yes.

Where do I get it?

Right here!

olua-0.0.1.tar.bz2 4 kB

The source code.