Under Review
over 2 years ago

Type Checking is incompatible with use of `self` keyword

At the beginning of the Objects, Modules, and Memory page in the Monkey C documentation it says:

Within a method implementation you can refer to your current instance using either the self or me keywords.

For example,

class A
{
public var x;
public var y;
public function initialize() {
me.x = "Hello"; // Set current instance x variable
self.y = "Hello"; // Set current instance y variable
}
}

However, doing so causes the Type Checking feature of the compiler to issue warnings, such as 

Cannot find symbol ':y' on type 'self'

It seems that the type checker is incompatible with this feature of Monkey C.  Can the type checker be fixed to work with me or self?

  • After further testing, I realize this is only an issue when a base class is referencing a method or variable that  is implemented only in subclasses. For example

    Isn't this expected behavior though? In a strongly-typed language like Java, this would also be an error because:

    1) The base class in principle isn't supposed to know that a subclass implements doX()

    2) More importantly, (in this case) the base class has no way of guaranteeing that all subclasses will implement doX(). Given that doX() is absent from the base class, this means the type checker cannot prevent a run-time error due to the possible absence of doX(0.

  • After further testing, I realize this is only an issue when a base class is referencing a method or variable that  is implemented only in subclasses. For example

    class AbstractBase {
    function initialize() {
    self.doX();
    }
    }

    class Sub extends AbstractBase {
    function doX() as Void {
    // do something
    }
    }


    This results in 

    Cannot find symbol ':doX' on type 'self'