albertodemichelis / squirrel

Official repository for the programming language Squirrel
http://www.squirrel-lang.org
MIT License
912 stars 156 forks source link

_inherited meta-method does not work as described in documentation. #239

Closed iSLC closed 3 years ago

iSLC commented 3 years ago

The documentation says:

invoked when a class object inherits from the class implementing _inherited the this contains the new class.

However, this does not point to the new class. It points to the same class where the meta-method is located. Which is kind of pointless if you ask me. You basically have a static method which gets invoked whenever someone extends that class but you don't know who extends you. This makes it the most useless feature feature as it serves no purpose.

Example:

class Base
{
    function constructor()
    {
        print("Base constructor");
    }

    function _inherited(attr)
    {
        print("Base: " + typeof(this));
        local o = this.instance();
        print(type(o));
        o.constructor();
        this.Hello();
    }

    static function Hello()
    {
        print("Hello from Base");
    }
}

class Derived extends Base
{
    function constructor()
    {
        print("Derived constructor");
    }

    function _inherited(attr)
    {
        print("Derived: " + type(this));
        local o = this.instance();
        print(type(o));
        o.constructor();
        this.Hello();
    }

    static function Hello()
    {
        print("Hello from Derived");
    }
}

Result:

 Base: class
 instance
 Base constructor
 Hello from Base
iSLC commented 3 years ago

Actually never mind. You do get the derived class. But the members exist in if you use it after it has been fully declared. At the time when _inherited is called on base you basically have a derived class that is identical to base.