Lua functions with colon
Asked Answered
lua
T

2

12

I am having a problem fully understanding the lua syntax so while this answer may be simple perhaps some authorative references will help me and others further learn.

function blah()

and

function classname:blah()
Teeters answered 20/8, 2015 at 23:58 Comment(2)
possible duplicate of What does ":" mean in LuaAntalkali
I think this question could be improved if it was edited to contain a question. :-)Disaster
C
30

Aubergine18's post covers the answer, but I'll explain from first principles to provide further clarification.

In Lua, functions are values, just like strings or numbers. This expression:

function() end

Creates a function value. You can assign this to a variable, just as you would any other value:

foo = function() end

Lua provides various short-cut syntaxes, also called "syntax sugar", for working with function values. The first is this:

function foo() end

This is exactly equivalent to:

foo = function() end

The other is:

function bar.foo() end

Which is exactly equivalent to:

bar.foo = function() end

In this example, bar is a table, foo is a key in that table, and the function value we created is the value assigned to that key.

Note that if you call foo:

bar.foo()

The function has no way of knowing that it was stored in the table bar using the key foo. If you want to treat that function as a method on the object bar, you need to provide it access to bar in some way. This is typically done by passing bar as the first parameter. By convention in Lua this parameter is named self:

function bar.foo(self) end

bar.foo(bar)

As a shortcut for this convention, Lua provides the following syntax sugar via the : operator:

function bar:foo() end

bar:foo()

This is exactly equivalent to the previous code.

Chamber answered 21/8, 2015 at 17:52 Comment(3)
Minor correction in the last code sample above... should be bar:foo() (the colon required to make Lua automatically pass 'bar' as first parameter to 'foo' function.Geopolitics
Great explanation. One question: if I call bar:foo() given that bar is silently passed into the method body, how do I call on bar? Is it actually self?Burny
@Burny Yes. function bar:foo() end is literally translated into function bar.foo(self) end, so foo has a first parameter named self no matter what other parameters you do (or don't) give it. bar:foo() is literally translated into bar.foo(bar), so inside of foo, self will contain bar.Chamber
G
7

When you call a function using colon notation, like this:

foo:bar()

Lua treats it like this behind the scenes:

foo.bar(foo)

If you define your function using dot notation, you'll have to manually specify the 'self' argument:

function foo.bar(self) ... end

However if you use colon notation, Lua will add an invisible 'self' parameter for you:

function foo:bar() ... end

Even though you don't see the self argument, it's there behind the scenes.

Basically colon notation is just a way to make your code look cleaner.

See also: Lua: colon notation, 'self' and function definition vs. call

Geopolitics answered 21/8, 2015 at 1:31 Comment(2)
How come some functions do not have foo at all and they just put bar? What does that then mean? Also self is referring to an instance of the class? I am prettty much new to Lua. Why do I need self?Teeters
If you have foo.bar() or foo:bar() then the function is just a property on the 'foo' table. It doesn't really matter where you put functions... They can be locally defined local function bar() ... end, globally defined function bar() ... end or defined as a property of a table, function foo.bar() ... end. The self stuff isn't needed, unless you need it heh. If you don't need a reference to the table or object your function is a property of, then you don't need to use the colon notation or self.Geopolitics

© 2022 - 2024 — McMap. All rights reserved.