Embedded class declaration

What’s the effect of a class defined within class definition body (eg DataMessage in the MessageManager library class) ? Is it restricting the scope of the embedded class so that it can only be instantiated by methods within the ‘parent’ class ?

Any insight from the various Squirrel guru’s on this forum ??

I suspect people missed this… going to mention a couple of them here @betsy @peter

Defining a class within another class limits the scope of the embedded class. See the example below.

Take the following classes:

class Parent {
    
    constructor() {
        server.log("in parent constructor");
    }
    
}

class Parent.Child {
    
    constructor() {
        server.log("in child constructor");
    }
    
}

These will create an instance of the child class:

local c = Parent.Child();

local p = Parent();
local pc = p.Child();

This however will fail with the following error “ERROR: the index ‘Child’ does not exist”:

local c = Child();

In libraries we use embedded classes for things like messages, tasks, auth, etc, to create a component that is used by the parent class. ie in MessageManager has an embedded message class to create a message for use by the parent class, Scheduler has an embedded job class, to create a job that is then scheduled to occur.

It namespaces the class within the enclosing class, but, as Betsy’s code demonstrates, there’s no encapsulation: it’s not just parent-class methods that can instantiate the nested class. Any Squirrel can access it. In C++ terms, it’s as if everything in Squirrel classes is always public. You can even extract the child-class and use it as if it were a top-level class:

local Adopted = Parent.Child;
local bob = Adopted();

Peter