Mainframe programmer playing with Codea.

This might be a strange question, but my daily work is on mainframes coding in Cobol and PL/1, so Codea is a new world to me (and a fun one). I have been through the different links like http://lua-users.org/wiki/TutorialDirectory, http://www.lua.org/pil/ and so on, and simply just trying to play with the different samples. There is a lot of things I don’t know about Codea, but could someone please tell me what SELF is ? I can see that variables inside a class is referenced by SELF.

Self references the current instance of a class – the object, rather than the template.

So if I had a class with a property of x, I could use self.x to look at the value of x in that instance. For example, suppose that MyClass has an init that looks like this.

function MyClass:init(x)
    self.x = x
end

Then in your program, you created a couple of instances of the class like this:

instance1 = MyClass(10)
instance2 = MyClass(20)

Now, when you’re coding outside the class, you can reference the value using the instance name, like instance1.x. Inside the MyClass class definition, you reference the same value as self.x.

In object oriented languages, a class defines the state of an object (member variables) and the methods that operate on that state (member functions). Objective-c, Smalltalk, and other OO languages use “self” as a special keyword within a class’ member functions to reference the object that those member functions are modifying (c++ uses “this” instead of “self”, but the idea is the same).

Lua is actually not an object oriented language. However, the creators of Lua have made it possible to more easily implement certain concepts of OO by using “syntactic sugar”, i.e. a kind of “shorthand” that reduces the amount of verbosity necessary to implement those OO concepts. One bit of syntactic sugar is the “:” operator when defining and calling a function. I’ll come back l this in just a second, but first it’s worth talking about how functions are implemented in Lua.

In Lua, functions are first class objects, like strings, numbers, tables, etc., which means that you can create a variable and assign a function to it. Another bit of syntactic sugar is that functions can be defined in two ways:

function doSomething()
     -- do stuff
end

is the same as:

doSomething = function()
     -- do stuff
end

In Lua, there is only 1 data structure, the table. A table is just an associative array that allows you to map values to keys. Lua also provides the “.” operator as syntactic sugar to to access variables within a table, allowing a table to seem as if it is an “object”. For example:

myTable = {}
myTable["aNumber"] = 10

is the same as

myTable = {}
myTable.aNumber = 10

Since functions are first class values, you can also assign functions to keys within a table:

myTable = {}

-- assign a function to the key
-- "doSomething" in myTable
myTable.doSomething = function()
     -- do stuff
end

To implement object orientation, a lot of times you will want a function that you assign to a table to operate on other variables in that table. To do so, you need to pass in a reference to that table as the first argument of that function:

myTable = {}

-- defining the function and assigning
-- it to myTable.doSomething
myTable.doSomething = function(table)
     -- do stuff to "table"
end

-- calling the function "doSomething"
-- in myTable, passing "myTable"
-- in so the function can do stuff to it
myTable.doSomething(myTable)

And now, finally to the aforementioned “:” syntactic sugar. In order to streamline the concept of passing a table to a function that you want to modify, the Lua creators decided it would be neat if there was a way to define a function that would implicitly pass the table that owns the function as the first argument to a function assigned to that table. When this happens, that implicit argument is called “self”:

function myTable:doSomething()
     -- do stuff to "self", which is an
     -- implicit reference to a table,
     -- because of the ":" syntax used to
     -- define the function
end

is really just shorthand for:

myTable.doSomething = function(table)
     -- do stuff to table
end

When you call the function using “:”, you are just saying “call a function on this table, and pass that table to the function as an implicit argument called ‘self’”:

myTable:doSomething()

is the same as:

myTable.doSomething(myTable)

Anyway, sorry for the longwinded answer. Hopefully it was informative and not too confusing.

@Mark: Thanks, I think I got now.
@toadkick: Thanks for the long anwer :slight_smile: It acctually helped me understand a little about how OO languages work. I think I can look at a class like a procedure in cobol and pl/1, you can pass information to a class and have it execute things, and you can get manipulated information back from it.
I can remember this class stuff from back in school were we worked with c++, but it is a very long time ago.
/Keld.

Well, it’s more than just a procedure…more specifically, a class could be thought of as a template for an object, defining a set of variables the object encapsulates, and a set of functions that operate on those variables. It sounds like you do have a better understanding though, so I’m glad I was able to help :slight_smile:

It’s worth noting though that with Lua you aren’t required to use OO. Lua is actually a much simpler and more flexible language that most (if not all) OO languages, and you have a lot of freedom to decide which programming paradigms you want to use. It also incorporates a lot of ideas from functional languages, like Lisp (first class functions and closures, only 1 major data structure(lists in Lisp, tables in Lua)), and imperative languages, like C, and prototype-based languages, like JavaScript (interestingly, JavaScript also implements objects using associative arrays, and provides the “.” syntactic sugar).

Codea does provides a good implementation of classes though, which makes it easier to learn some OO concepts, and makes it easier for people who are familiar with other OO languages to use Lua.