18

I have a problem with classes. I got below error: Attempt to index local 'self' (a nil value) When I call the getter method of below class. Item.lua file:

require "classlib"
Item = class("Item")

function Item:__init()
    self.interval = 1
end

function Item:getInterval()
    return self.interval
end

I'm calling this getter function like this:

dofile("../src/item.lua")

item = Item()

function test_item()
    assert_equal(1, item.getInterval())
end

What's the problem here?

Kind regards...

Michal Kottman
  • 16,375
  • 3
  • 47
  • 62
zontragon
  • 780
  • 1
  • 9
  • 27
  • Call `item:getInterval()` instead of `item.getInterval()` inside `test_item()`. – Omri Barel Dec 28 '12 at 12:14
  • Might be useful to tell what is this "classlib". Notice you have unmatching quotes (single vs. double) in the `class()` call. And @OmriBarel should probably post an answer, instead of a comment... :-) – PhiLho Dec 28 '12 at 12:35
  • Thank you! What is the difference between them? – zontragon Dec 28 '12 at 12:42

3 Answers3

37

In general, you should call member functions by :.

In Lua, colon (:) represents a call of a function, supplying self as the first parameter.

Thus

A:foo()

Is roughly equal to

A.foo(A)

If you don't specify A as in A.foo(), the body of the function will try to reference self parameter, which hasn't been filled neither explicitly nor implicitly.

Note that if you call it from inside of the member function, self will be already available:

-- inside foo()
-- these two are analogous
self:bar()
self.bar(self)

All of this information you'll find in any good Lua book/tutorial.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
1

the obj:method is just syntactictal sugar for:

definition:

function obj:method(alpha) is equivalent to obj.method(self,alpha)

execution:

obj:method("somevalue") is equivalent to obj.method(obj,"somevalue") Regards

Alar
  • 760
  • 4
  • 11
-1

Change:

assert_equal(1, item.getInterval())

to:

assert_equal(1, item:getInterval())

In Lua, it was some ridiculous for error reporting. From class point of view, the .getInterval() method should called with a self parameter, while the :getInterval() method is implicitly included the self parameter. And the syntax error should labeled in the called point, not the definition-body of getInterval().

In traditional, while you miscalled a method, it was not the method's fault, but the caller.

coanor
  • 3,746
  • 4
  • 50
  • 67
  • Your answer doesn't really bring any new information in, and it's partially incorrect. *the syntax error should labeled in the called point* - it would be, but it's not a syntax error. `o.f()` is still valid Lua code, it just does something different from the colon call. – Bartek Banachewicz Jan 16 '17 at 07:25