WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Help with Lua OO (https://www.wowinterface.com/forums/showthread.php?t=56936)

mtp1032 12-31-18 07:01 PM

Help with Lua OO
 
Here is an error I'm having trouble tracking down.

Message: Interface\AddOns\SandBox\libs\UnitTests.lua:38: attempt to call method 'is_a' (a string value)

Here's my class declaration followed by the _init(...) definition

Lua Code:
  1. Slot = {}
  2. Slot.__index = Slot
  3.  
  4. setmetatable(Slot, {
  5.     __index = Container,        -- makes the inheritance work
  6.      __call = function (cls, ...)
  7.     local self = setmetatable({}, cls)
  8.     self:_init(...)
  9.     return self
  10.   end,
  11. })
  12.  
  13. -- This is the _init function.
  14. function Slot:_init( bagNumber, slotIndex )
  15.            
  16.     Container._init(self)               -- call the base class constructor
  17.     self.is_a = "Slot"                   -- in the parent class, this is set to "Virtual Container"
  18. end
  19.  
  20. -- the is_a() method
  21. function Slot:is_a()
  22.    return self.is_a
  23. end

Now, here's the test code that elicits the error:

Lua Code:
  1. local s = Slot(1,1)
  2. DEFAULT_CHAT_FRAME:AddMessage( s:is_a() )

It fails with the message shown above. Both function and property work as expected in the base class.

Any thoughts?

Fizzlemizz 12-31-18 07:59 PM

Code:

function Slot:is_a()
adds the method to the table when it is "built". Later when you call Slot:_init()
Code:

local s = Slot(1,1)
Code:

self.is_a = "Slot"
overwrites the function(method) with the string causing
Code:

DEFAULT_CHAT_FRAME:AddMessage( s:is_a() )
to try and run it as a method when it's now a string.

mtp1032 12-31-18 11:17 PM

Well, thanks as always, Fizzlemizz but I'm not sure I understood your suggestion. For example, I rewrote the method and changed its name from this (the original):
Code:

function Slot:is_a()
    return self.is_a
end

to this (note the changed name):
Code:

function Slot:type()
    return self.is_a
end

and I still get the same error, i.e., interpreting 'type' as a string.

So, after all this, did I misunderstand your suggestion?

Cheers,

Fizzlemizz 01-01-19 12:06 AM

Your original code with a few print statements

Lua Code:
  1. Slot = {}
  2. print("Slot =", Slot)
  3. Slot.__index = Slot
  4.      
  5. setmetatable(Slot, {
  6.         __index = Container,        -- makes the inheritance work
  7.          __call = function (cls, ...)
  8.         local self = setmetatable({}, cls)
  9.         self:_init(...)
  10.         return self
  11.       end,
  12. })
  13.      
  14.     -- This is the _init function.
  15. function Slot:_init( bagNumber, slotIndex )
  16.                
  17.         --Container._init(self)               -- call the base class constructor
  18. print("self =", self)
  19.         self.is_a = "Slot"                   -- in the parent class, this is set to "Virtual Container"
  20. end
  21.      
  22.     -- the is_a() method
  23. function Slot:is_a()
  24.        return self.is_a
  25. end
  26.    
  27. local s = Slot(1,1)
  28. print("Finally s =", s)
  29. print("Value of is_a", s.is_a)

At each print (excluding the last) you will notice the the table is the same. That means that during the intitialisation of s (local s = Slot(1,1)) when the code gets too:
Code:

self.is_a = "Slot"
you're actually doing
Code:

Slot.is_a = "Slot"
In effect overwriting the original funtion declaration of:
Code:

function Slot:is_a()
      return self.is_a
end

and turning it into the string "Slot"

I think you're thinking that s should actually be a new table entry in the Slot table.

mtp1032 01-01-19 07:32 AM

Quote:

Originally Posted by Fizzlemizz (Post 331221)
I think you're thinking that s should actually be a new table entry in the Slot table.

You're right! That's exactly what I am (was) thinking. I'll need to sketch the tables to grok this.

Thanks and cheers,


All times are GMT -6. The time now is 10:47 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI