cascading function call scope?

#dumbQuestionAlert

This seems strange to me. Just checking if this is expected behavior. It does happen in both the sim and device, so I'm guessing it is a user error (me).

In Class "A" I have function X which calls function Y. Both X and Y are in Class A. No problem. I don't have to use the class name in the function call, I just need the function name "Y()", since both are in the same class. Basic stuff. Works great.

In another source file, in another Module, I call function X. I do have to specify the class name this time. So I call A.X(). That call works.

However, I then get a "symbol not found" error for Y. I would think since it is X calling Y, both in the same class, I would not need the class name. X should know how to get to Y.

Anyway, I assume this is expected behavior. And I just should get in the habit of always preceding a function call with it's class or module name?
  • I am just making an educated guess, but it sounds to me like your code looks like this:

    class A
    {
    function x()
    {
    System.println("x");
    y();
    }
    function y()
    {
    System.println("y");
    }
    }

    class TestDataFieldView extends Ui.SimpleDataField {

    function initialize() {
    SimpleDataField.initialize();

    var a = new A(); // create instance of A
    a.x(); // a.y() is called successfully; both "x" and "y" are printed.

    A.x(); // Call x() without an instance
    // A.y() is not called successfully; only "x" is printed,
    // then the app crashes with "Could not find symbol y"

    }


    If that's the case, then the real bug, IMO, is that you were allowed to call A.x() in the first place. (I vaguely recall some discussion or mention of this at one point, as a matter of fact.)

    If you want to call a class function without a class instance (e.g. "var a = new A()"), then you need to make your function static. This means that it does not require an instance (and thus, has no access to instance functions / data in the class).

    Any class function called by a static class function should also be declared as static. Same with any class variable referenced by a static function.

    Sure, the initial call to A.x() works without the static qualifier, but I don't think that is 100% correct behaviour.

    class A
    {
    static function x() // I can be called without an instance
    {
    System.println("x");
    y();
    }

    static function y() // I can also be called without an instance
    {
    System.println("y");
    }
    }
  • Yep, I'd agree that you shouldn't be able to call a non static member just through a class reference. But given that it's letting you, makes total sense not to be able to call a member function as there is no instance object to reference.
  • It makes sense, but it’s failing at a point which could be more confusing than if it failed at initial call where it should. I’m guessing this is a consequence of how message passing / scopes are implemented in Monkey C. I wish I could remember where I read about this quirk before, but it doesn’t matter. As you pointed out, as soon as you try to reference anything else that’s non-static, the app will crash.
  • Thank you for the refresher! Yeah, I've been leveraging this "feature" without understanding the consequences. Will fix my code.