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 ‘.’ ?
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