Ticket Created
over 5 years ago

WERETECH-9125

Use of nested classes in Monkey C

I am trying to set up a data structure of an ANT device, and would like to use a sytem of nested classes for the data structure, so for example you would have a sensor data object, with a "page 20" property, which itself has "event count", "temperature" etc properties.  There are multiple pages in the ANT profile so being able to do this would keep things simpler than a flat data structure.

However this is giving a runtime error on initialization of the parent class.  Example code below gives error: Could not find symbol 'child_class'

Code below shows .  What am I doing wrong?

*** EDITED - Typo in original post, but not source of problem.  Code simplified to illustrate issue ***

    class child_class
    {
    	var x;
    	var y;
    	function initialize()
    	{
    		x=0;
    		y=0;
    	}
    }
    
    class parent_class
    {
    	var z;
    	var child;
    	function initialize()
    	{
    		z=0;
    		child = new child_class();
    	}
    }

  • I moved it from Discussion to Bug Reports. I think that may be why it disappeared from your profile. As for the missing comments, that was human error... I inadvertently failed to check the box to migrate comments. Sorry.

  • Thank you - that is exactly it - I am creating these classes inside of a view class.  I will try the static approach.

    This post has disappeared from my profile, and all other comments have disappeared.  Do you know why?

    Best regards

    Barney

  • I'm able to reproduce this error if the above declarations are put inside of another class:

    class AClass {
    
        class child_class
        {
            var x;
            var y;
            function initialize()
            {
                x=0;
                y=0;
            }
        }
        
        class parent_class
        {
            var z;
            var child;
        
            function initialize()
            {
                z=0;
                child = new child_class();
            }
        }
    
        var bob;
        function initialize() {
            bob = new parent_class();
        }
    
    }
    
    
    class bogusView extends WatchUi.View {
    
        function initialize() {
            View.initialize();
            
            var p = new AClass();
        }
    }

    I've filed a bug for this. I've also found that you can work around the problem by adding a static declarator on each of the nested class entries. I'm not certain that this won't have additional side-effects, but it does work for this trivial case.

    class AClass {
    
        static class child_class
        {
        // ...
        }
        
        static class parent_class
        {
        // ...
        }
    
    // ...