Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Possible bug: self in base class init function is not equal to self in the derived class init — Gideros Forum

Possible bug: self in base class init function is not equal to self in the derived class init

ndossndoss Guru
edited April 2012 in Bugs and issues
From: http://www.giderosmobile.com/forum/discussion/806/inheritance-troubles

I believe this is a bug and didn't want it to get lost so I figured I'd repost it:
A = Core.class()
function A:init()
   print(self)
end
 
B = Core.class(A)
function B:init()
   print(self)
end
 
local b = B.new()
The value of self in the two init functions are different when "b" is created. This is not what most people would expect and can cause subtle & difficult to find problems.

--ND

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited April 2012
    sorry, can't think of right example on the spot, but if it inherits all parent's properties and methods, does it really matter if they are different or not?
  • The original post has one example (setting up an event listener in the base class).

    Another example...
    function base:init(parent)
          parent:addChild(self)
    end
  • Cannot see any bug here. It works as expected. What are you trying to do?
  • @Michal, here's a more complete example ...
    A = Core.class(Sprite)
    function A:init()
       stage:addChild(self)
       aSprite = ... -- make "aSprite"
       self:addChild(aSprite)
    end
     
    B = Core.class(A)
    function B:init(parent)
       bSprite = ... -- make "bSprite"
       self:addChild(bSprite)
    end
     
    local b = B.new()
    I'm away from my computer, so I can't test this yet, but I don't believe "bSprite" would be seen since the "self" created in B:init isn't the same as the self created in A:init when creating "b". The "self" created in B:init is never added to the stage.

  • atilimatilim Maintainer
    edited April 2012
    As you remember, previously with Gideros' class system it was not possible to create your own base classes or inherit from your own classes. Therefore, a simple deep copy mechanism worked well. But after extending, deep copy is causing some problems in some specific cases (as in registering events).

    Let me change deep copy to shallow copy and test extensively.

    @ndoss, your latest example works well. Although they are different selfs, they refer to the same Sprite instance internally on C++ side.

    (And honestly I don't remember why I preferred deep copy instead of shallow :) )

    Thank you

  • Thanks for looking at it Atilim
  • atilimatilim Maintainer
Sign In or Register to comment.