Advanced class features

The built in class() function is nice, but it is lacking some more advanced features I would like to see: Human readable names, mixins, and the ability to directly refer to the superclass.

Right now I use a modified version of MiddleClass (since it also creates a function called class()it can break existing code unless modified). But it would be nice if you were to add a .super member and and a .mixin()method as well. That would remove the the primary reasons I use MiddleClass.

I’ve wanted a .super method for awhile now. I’ve made a couple of attempts to implement it myself but so far have not come up with a clean and efficient way to. Thanks for pointing out MiddleClass, I look forward to playing with it!

At the very least, I too would love it if Codea’s class function could support these features.

Is the only modification you’ve made to MiddleClass renaming the class() function to prevent the naming clash? If not, what other modifications have you made? Also, out of curiosity, what did you rename MiddleClass’ class() function to?

@toadkick, yes the only change I made to MiddleClass was to rename class() to mclass(). However since Lua is case sensitive you could also use Class() if you wanted

As an aside I have tried a number of class systems for Lua and MiddleClass is my favorite. It is small, self contained, and feature-full. It is conceptually similar to my favorite class system for JavaScript, LineageJS.

I went with mclass as well. Even though Lua allows it I prefer to avoid the same names with different case to avoid confusion. I also changed initialize() to init() as I prefer the shorter version and the orthogonality with Codea’s classes.

All in all I like this implementation. I realize now that MiddleClass provides a .super method for the class, but the instantiated object does not have a corresponding .super method, so I still need to qualify my superclass by name in overriden methods when I want to propagate the call up the inheritance chain (which is okay, but I like that languages like Objective-C include that functionality). Even still, mixins, human readable class names, and access to a class’ superclass is still a win in my book :slight_smile: Thanks again for the tip!

You are most welcome. Yeah I wish super applied to the object and not just the class. One of these days I plan on adding that.

So I’ve had a pretty good time tinkering with MiddleClass over the last couple of hours. Eventually I came to have a couple of minor nits to pick with it:

a) the instanceOf(), subclassOf(), and includes() functions were in the global namespace, instead of being member functions to Object and Object.static

b) the args to those functions were in the reverse order of what made sense to me (which incidenly would be an order that would allow them to support the : function calling syntax should one choose to make them member functions).

So, I forked it, made those functions local to the module, swapped the args, and made them member functions in the Object and Object.static tables to enable:


which I think is cleaner and reads more fluidly than:

instanceOf(Class, anObject)
 subclassOf(OtherClass, Class)
 includes(mixin, Class)

The forked version is at if anyone is interested.

By the way, what would be a kosher way to handle the license for this since I’ve modified it? I don’t want to give the impression that this code as-is is Enrique García Cota’s, but I think it seems fair to note the modifications to it.

My pleasure!

About the license, great, that’s what I’ll do. Thanks!

Good stuff thanks for doing that!

As for the licence I would suggest you put your own copyright statement that is co-equal with the existing one (you need to preserve it any way) and say “Additional changes Copyright 2012 By…” and include your name