Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Problems with getClass - Gideros Forum

Problems with getClass

Hi,

My code implements a sort of assertions system for validating functions arguments.
I cannot determine the real class of an object. Imagine a hierarchy as the following
MyView = Core.class(Sprite)
...
MainScreen = Core.class(MyView)
Object -> Sprite -> MyView -> MainScreen

Calling getClass() and getBaseClass() on a MyView object the runtime returns respectively: Sprite and Object.
Even this tests fails
obj:isInstanceOf("MyView")
or
obj:isInstanceOf("MainScreen")
Any help?
Thank you all

Mauro

Comments

  • tetnictetnic Member
    edited October 15
    I have printed the entire metatable of an object and it is clear that the field
    __classname
    is set only for Gideros' classes. For example
    __classname= "Sprite"
    I'm creating a workaround function that returns a subclass and sets the field. Hope it will work.
  • hgy29hgy29 Maintainer
    Thanks for reporting,
    I can see why Gideros doesn't set it for lua classes, it is because it has no way of knowing how you named your class! That could be added as a third arg to Core.class maybe ? Second arg is already used for base class parameters.
    MyView = Core.class(Sprite,nil,"MyView")

    Likes: tetnic

    +1 -1 (+1 / -0 ) Share on Facebook
  • tetnictetnic Member
    edited October 15
    Hi @hgy29,

    Yes that is the solution I was trying to implement as a workaround :smile:
    function Core.class(super, subclass)
     
        local cls = Core.class(super)
     
        -- Set __classname on metatable
     
        return cls     
     
    end
  • hgy29hgy29 Maintainer
    Thinking of it, it would be probably better to accept the class name as second argument (based on its type), and the base class constructor function as second or third arguments.
  • tetnictetnic Member
    edited October 15
    In my function the "subclass" name is mandatory, only to force every class to have a __classname. But I think that having the choice could be the best solution.

    I have also searched for info about macros... but I cannot make it compile. Is it yet available the "macro" plugin? It seems that it can permit to create some interesting macros as the following:
    '<a href="/profile/def">@def</a> Class(class, super) class = Core.class(super, nil, _STR_(class))
    '
  • hgy29hgy29 Maintainer
    macros are builtin gideros, so no plugin required, but I am not the right guy to explain how to use them (I don't).
    This can help you however: https://wiki.giderosmobile.com/index.php/Macro_Functions

    Likes: SinisterSoft

    +1 -1 (+1 / -0 ) Share on Facebook
  • tetnictetnic Member
    edited October 18
    Thank you, I already saw that, but is not useful because it seems to not supporting the stringify mechanism.

    I'm basing on this post, if it's valid yet: http://forum.giderosmobile.com/discussion/comment/48204/#Comment_48204

    But I'm not able to make it work.

    However I've already implemented my helper functions:
    function Core.subclass(super, args, subclass)
    	Assertions.argNotNil(super, "super", Type.isClass(super))
            Assertions.arg(args, "args", Type.isFunction(args))
     	Assertions.argNotNil(subclass, "subclass", Type.isString(subclass))
     
    	local cls = Core.class(super, args)
    	Core.setClass(cls, subclass)
     
        return cls     
    end
     
    function Core.setClass(value, class) 
    	Assertions.argNotNil(value, "value", Type.isTable(value))
     	Assertions.argNotNil(class, "class", Type.isString(class))
     
    	local mt = getmetatable(value)
            if not mt then 
                error("Invalid superclass.")
            end
    	mt["__classname"] = class
    end
    I have used my assertions classes inside them... If anyone will like I can share them on GitHub.

    Mauro
Sign In or Register to comment.