View Single Post
01-01-19, 12:06 AM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
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.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 01-01-19 at 12:51 AM.
  Reply With Quote