.lua tutorials (9)

1 Name: #!/usr/bin/anonymous : 2008-02-14 22:07 ID:23yU/iGN

Hello there. Ive had some experience with c++ an Python, but I would like to add Lua to my skills. Does anyone know where can I get some decent tutorials?

Also, some thoughts on the laanguage itself can be useful. Thanks.

2 Name: dmpk2k!hinhT6kz2E : 2008-02-15 02:45 ID:Heaven

I prefer the tutorials (http://lua-users.org/wiki/TutorialDirectory) and Lua Reference Manual (http://www.lua.org/manual/5.1/) together over the online Programming in Lua. I've never read the 2nd edition of PiL, so perhaps it's an improvement. You'll probably have to read PiL eventually, but the first two will take you a very long way.

I like the language a fair bit. Unlike many popular dynamically-typed languages ahem it gets the lexical scoping right, and unlike some it also has first-class functions; the closures combined with build-you-own OO is very powerful. I like tables as the only aggregate data type, although the abstraction leaks a little if you compare finding the number of elements in an array versus the number of keys. It also keeps a statement/expression dichotomy, alas.

The metatables are powerful and allow you to build any OO system you want -- although since you can only attach one metatable to types other than tables (and userdata?) it's not turtles all the way down.

Also, I wish it were a bit more terse. "function()" and "local" annoy me. On the flip side there's an unusual project, called MetaLua, which lets you do Lisp-like things to the language. If you don't like some syntax, you can use MetaLua to change it.

Implementation-wise, the Lua people have everything right. Lua's implementation is a sterling example of how to implement an interpreter, and puts everything else out there to shame. If you want to learn how to implement a VM, Lua is what you should look at. It's probably no wonder people like Mike Pall -- the fellow working on LuaJIT -- have been attracted to Lua.

Lua has some notable drawbacks though. It's small, fast and portable... but the "small" in particular has ensured the standard library is pretty thin. If you're expecting to do anything serious with Lua you'd best brush up on your C, because you'll be using the C a lot to bind to other things. Fortunately the C API is easy, but if you're not the kind of person who builds things from scratch or needs an embedded language, best look elsewhere.

Also, while Lua has 8-bit clean strings it doesn't have any means of manipulating Unicode, the regular expressions it provides are decent but nothing like Perl's, and the numbers are doubles -- no bignums or the like.

3 Name: #!/usr/bin/anonymous : 2008-02-15 11:30 ID:Heaven

> turtles all the way down

Ok, that's like the second or third time I've seen that phrase on this board. WTF does it mean?

4 Name: #!/usr/bin/anonymous : 2008-02-15 11:46 ID:T54EZGYF

>>3

It's a systems programming thing. You make a call to the turtle stack via one of the four "elephants", which essentially support the entire disc.

5 Name: #!/usr/bin/anonymous : 2008-02-15 15:44 ID:easVEf3W

>>3 dmpk2k is saying that it isn't metacircular; that there exists some more primitive thing in the language that isn't like anything you're exposed to at the top. In this case, the magic associated with metatables cannot be recursed into metatables themselves. The reference comes from cosmology, where the story goes:

"A cosmologist gave a public lecture on astronomy. He talked about how the earth went around the sun, and all the orbits of the planets, how the galaxies have their own orbits, and how the universe is all expansive and so on.

At the end of the lecture, an old lady in the back said "That's garbage. The world is really a flat plate on the back of a giant turtle"

The cosmologist smugly replied, "What's the turtle standing on?"

To which the old lady says, "You're very clever, but it's turtles all the way down."

6 Name: dmpk2k!hinhT6kz2E : 2008-02-15 18:33 ID:Heaven

That's pretty close, except that metatables can be recursed. A metatable is just a normal table being used in a special way.

The problem is with the other data types. Let's say I want to make my own OO system: for this I'd attach metatables to tables. I can attach a unique metatable to a unique table if I want. I can do this with userdata too.

I cannot do that with numbers, threads, strings, or any other data type. While I can attach metatables to them, all instances of each data type must share the same metatable.

So it's possible to build any style of OO system -- that I'm aware of -- with tables and userdata, including varieties of prototype. All the other data types are stuck with a class-based OO, which won't allow you to change behaviour on individual objects.

See here for the gritty details: http://www.lua.org/manual/5.1/manual.html#2.8

7 Name: #!/usr/bin/anonymous : 2008-02-15 21:48 ID:Heaven

>>6 What I meant was that when the lua interpreter uses the metatable, it ignores any magic associated with it. Am I wrong on this? Can call have a meta-call?

> I cannot do that with numbers, threads, strings, or any other data type. While I can attach metatables to them, all instances of each data type must share the same metatable.

Pretend that you've just made a bunch of classes with one instance each, and that with numbers, threads, strings, etc, are the norm.

If it's really bothersome for 4+5 to be different than 5+4, you could always create a new numberish thing that printed "4", but was really just a table.

> So it's possible to build any style of OO system

It seems that lua lacks the ability to make a clos-like system- generic functions seem impossible given the lack of a real typeof operator.

8 Name: dmpk2k!hinhT6kz2E : 2008-02-17 18:13 ID:Heaven

> Can call have a meta-call?

Yes. It's a bit ugly since there's no way that I'm aware of to refer to the constructed table inside the construction itself -- thus moving the __index assignment in var2 outside.

var1 = {}
var2 = {}
var2.__index = var2
var3 = { __index = var3, foo = function() print "bar" end }
setmetatable( var1, var2 )
setmetatable( var2, var3 )
var1.foo()

This'll print out "bar".

> you could always create a new numberish thing that printed "4", but was really just a table.

This is certainly true. It's more an aesthetic issue for me, since I like things to be consistent.

> It seems that lua lacks the ability to make a clos-like system- generic functions seem impossible given the lack of a real typeof operator.

I'm not familiar with CLOS, MOP and all that madness (although I hope to one day!). Lua does have a type() function, which returns the primitive data type (e.g.: "string", "thread"). If you're trying to find the class or object itself, you can use a similar technique to var2.__index above in a new method:

function create_class()
local new_class = {}
  new_class.class = function() return new_class end
new_class.__index = new_class
  return new_class
end
SomeClass = create_class() 
function SomeClass:new()
local instance = {}
  setmetatable( instance, self )
  return instance
end

Every time you'd call SomeClass:new() you'd get an object with a parent class SomeClass. Calling the class() method on the new object would return you SomeClass.

I'm not sure if that's adequate or not.

9 Name: #!/usr/bin/anonymous : 2008-02-18 00:46 ID:RZX99TNE

> Yes. It's a bit ugly since there's no way that I'm aware of to refer to the constructed table inside the construction itself

Interesting.

> I'm not familiar with CLOS, MOP and all that madness (although I hope to one day!).

It's wonderful. I suggest you spend some time hacking it- it may do well to start by trying to implement part of generic functions in lua (you seem to be familiar enough). You might not be able to get it to work with other peoples class systems, but you might be able to make a class system with enough information in its metatable to implement it.

> Lua does have a type() function,

which prints "table" in the most useful case. :-/

This thread has been closed. You cannot post in this thread any longer.