Library inclusion

Is it just me (and my thick head) or is it strange that calling a function on a table is done using ‘:’ but calling a function in a library (as loaded via ‘require’) is done using ‘.’ ?

matt. [import]uid: 8271 topic_id: 4292 reply_id: 304292[/import]

http://lua-users.org/wiki/ObjectOrientationTutorial

Method declarations

Likewise there is syntactic sugar for method declarations. Regardless how it is declared, a method expects that first argument passed in is the object to be acted on. If a dot is used (i.e. t.foo(self, args)) we declare self ourselves. If a colon is used (i.e. t:foo(args)) self will be declared automatically for us.

> t = {}
> function t.foo(self,x) print(self,x) end – no sugar, explicit self
> function t:foo2(x) print(self,x) end – sugar and magic self
>
> t.foo(t,1) – is the same as…
table: 0035D830 1
> t:foo2(1) – shorthand for above
table: 0035D830 1
And reversed:
> t:foo(1) – the same as…
table: 0035D830 1
> t.foo2(t,1)
table: 0035D830 1

[import]uid: 6645 topic_id: 4292 reply_id: 13334[/import]

and…

http://onlamp.com/pub/a/onlamp/2006/02/16/introducing-lua.html [import]uid: 6645 topic_id: 4292 reply_id: 13335[/import]

Ah yes, I /knew/ I’d read that somewhere!

Thanks,

Matt. [import]uid: 8271 topic_id: 4292 reply_id: 13340[/import]