1

Let's say we have two function declarations:

function MyData:New 
end

and

function New(MyData)
end

What is the difference between them? Does using : have any special purpose when it comes to inheritance and OOP? Can I only call functions declared with : by using :?

I'm coming from using only C# -- so if there's any comparison to be made, what would it be?

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
Catlard
  • 853
  • 6
  • 13
  • 30

3 Answers3

5

Adapted from the manual, end of ยง3.4.10:

The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. Thus, the statement

function t:f (params) body end

is syntactic sugar for

t.f = function (self, params) body end

Community
  • 1
  • 1
lhf
  • 70,581
  • 9
  • 108
  • 149
2

You should search SO as there are many questions about this but you have a set of questions so I can't say this is a duplicate.

Q. What is the difference between them? A. The one with colon causes a method to be added to the MyData table, and the Lua interpreter to automatically insert a "self" before the first parameter when called, with this "self" pointing to the MyData instance that the "method" is being called upon. It is the same as writing MyData.New = function(self) end. The second signature has a parameter called MyData and is a global function. It is unrelated to the MyData table (or class).

Q. Does using ":" have any special purpose when it comes to inheritance and OOP? A. No; it is merely syntactic sugar so that when you call MyData.New you can just write MyData:New() instead of the clunky looking MyData.New(MyData). Note that a "new" function is typically to create instances of a class, so you wouldn't have such a function be a method, rather just a function in the MyData table. For inheritence and OOP, you use metatables, and this does not interact with : in any special way.

Q. Can I only call functions declared with ":" by using ":"? A. No, as mentioned, it just syntactic sugar, you can define one way and call a different way.

Q. I'm coming from using only C# -- so if there's any comparison to be made, what would it be? A. For functions, the : is like the . in C#, whether used in a call or definition. The "." in Lua is more like "attribute", there is no equivalent in C# for functions.

MyData = {} -- a table 
function MyData.func(self) 
    print('hello')
end
MyData:func()
MyData.func(MyData) -- same as previous

function MyData:func2() -- self is implicit
    print('hello')
end
MyData:func2()
MyData.func2(MyData) -- same as previous

Note that MyData as defined above is not a class, because you cannot create "instances" of it (without doing extra work not shown there). Definitely read the Programming in Lua online book on the Lua.org website, lots of useful discussions of these notions.

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
Oliver
  • 27,510
  • 9
  • 72
  • 103
1

Lua doesn't have functions declarations per se; It has function definition expressions. The syntax you have used is shorthand for a function definition expression and assignment.

The only difference in your examples is when the first statement is executed a new function is created and assigned to the field New in the table referenced by the variable MyData, whereas the second is an assignment to a non-field variable (local, if previously declared, otherwise global).

Keep in mind that these are only the first references to the created function values. Like any other value, you can assign references to functions to any variable and pass them as parameters.

If you add formal parameter usage to the bodies then there is another difference: The first has an implicit first parameter named self.

If you add function calling to the scenarios, the ":" syntax is used with an expression on the left. It should reference a table. The identifier to the right should be a field in that table and it should reference a function. The value of the left expression is passed as the first actual argument to the function with any additional arguments following it.

A function definition with a ":" is called a method. A function call with a ":" is called a method call. You can construct a function call to a function value that is a field in a table with the first argument being a reference to the table using any function call syntax you wish. The Lua method definition and method call syntax makes it easier, as if the function was an instance method. In this way, a Lua method is like a C# Extension Method.

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72