0

I'm still playing around with lua modules and I've found the following "interesting" issue that occurs depending on how you create your methods / functions inside a module. Note the following code in a file called test_suite.lua:

local mtests = {} -- public interface

function mtests:create_widget(arg1)
 print(arg1)
 -- does something
 assert(condition)
 print("TEST PASSED")
end
 return mtests

Using the above code, arg1 is always nil, no matter what I pass in when calling create_widget(). However, if I change the definition of the function to look like this:

function mtests.create_widget(arg1) -- notice the period instead of colon
 print(arg1)
 -- does something
 assert(condition)
 print("TEST PASSED")
end

then, the system displays arg1 properly.

This is how I call the method:

execute_test.lua

local x = require "test_suite"
x.create_widget(widgetname)

Can you tell me what the difference is? I've been reading: http://lua-users.org/wiki/ModuleDefinition

But I haven't come across anything that explains this to me. Thanks.

dot
  • 14,928
  • 41
  • 110
  • 218

3 Answers3

6

All a colon does in a function declaration is add an implicit self argument. It's just a bit of syntactic sugar.

So if you're calling this with (assuming you assign the mtests table to foo), foo.create_widget(bar), then bar is actually assigned to self, and arg1 is left unassigned, and hence nil.

foo = {}
function foo:bar(arg)
    print(self)
    print(arg)
end

Calling it as foo.bar("Hello") prints this:

Hello
nil

However, calling it as foo:bar("Hello") or foo.bar(foo, "Hello") gives you this:

table: 0xDEADBEEF (some hex identifier)
Hello

It's basically the difference between static and member methods in a language like Java, C#, C++, etc.

ECrownofFire
  • 472
  • 4
  • 11
0

Using : is more or less like using a this or self reference, and your object (table) does not have a arg1 defined on it (as something like a member). On the other way, using . is just like defining a function or method that is part of the table (maybe a static view if you wish) and then it uses the arg1 that was defined on it.

prmottajr
  • 1,816
  • 1
  • 13
  • 22
0

. defines a static method / member, a static lib, Which means you can't create a new object of it. static methods / libs are just for having some customized functions like printing or download files from the web, clearing memory and...

: Is used for object members, members that are not static. These members change something in an object, for example clearing a specified textbox, deleting an object and...

Metamethod functions(Functions that have :) can be made in lua tables or C/C++ Bindings. a metamethod function is equal to something like this on a non-static object:

function meta:Print() self:Remove() end

function meta.Print(self) self:Remove() end

Also, with . you can get a number/value that doesn't require any call from a non-static or static object. For example:

-- C:
int a = 0;
-- Lua:
print(ent.a)

-- C:
int a()
{
    return 0;
}
-- Lua:
print(ent:a())

same function on a static member would be:

print(entlib.a())

Basically, each non-static object that has a function that can be called will be converted to : for better use.

111WARLOCK111
  • 857
  • 2
  • 7
  • 14
  • So if i understand correctly, if I want to keep the ":" I should make a "property" that accepts and assigns a value to "arg1". is this correct? – dot Dec 18 '13 at 21:33
  • where can i get some additional documentation on this? – dot Dec 18 '13 at 21:33
  • @dot pull up the Lua reference http://www.lua.org/manual/5.1/manual.html and search for 'self' the first hit explains this. The book Programming in Lua (PIL) also discusses this (see lua.org for how to purchase). – Oliver Dec 18 '13 at 22:07