Cicada ---> Online Help Docs ---> Cicada scripting ---> Classes and Inheritance

Classes

A Cicada class, or class instance, is nothing more than a composite variable having both members to variables and members to methods (functions).


    myClassObject :: {
       data :: int
       moreData :: string
       
       init :: {
           code
           { data, moreData } = { 1, "blank" }
       }
       
       otherMethod :: { ... }
    }
   

In a sense, all we have done is point out that functions such as init() can be defined inside of composite variables.

To make our class object more C-like we can give it a proper constructor. All we need to do is take the constructor code from the init() method and put it somewhere after the class member definitions.


    myClassObject :: {
       data :: int
       moreData :: string
       
       { data, moreData } = { 1, "blank" }
       
       otherMethod :: { ... }
    }
   

Now both myClassObject and each class instance such as obj1 :: myClassObject begins initialized to { 1, "blank" }. We can even bundle the constructor code together with the member definitions:


    myClassObject :: {
       data := 1
       moreData := "blank"
       
       otherMethod :: { ... }
    }
   

We call these bits of code constructors because they run when the object is created. C++ constructors also run when an object is created, but they can also be rerun by calling a special function. How can a Cicada constructor be rerun? One way is simply to redefine the object:


    myClassObject :: myClassObject
   

but there is a better way. Just as in C++, we can also run the constructor as a function.


    myClassObject#0()   | code #0 is the constructor
   

It turns out that this same trick can also reinitialize members of functions, by rerunning everything up to the code marker. Remember that code number 1, the default function code, begins after the first code marker or semicolon; therefore the constructor code, which is code number 0, comprises everything leading up to that first code marker. In the case of a class object, there is no code marker, so the constructor is everything inside of the curly braces.

There is no destructor in Cicada. In fact there isn’t even a direct way to delete an object -- we can only remove members leading to an object. Only when Cicada discovers an object to be completely cut off from the user will dispose of the object and free its memory. (It’s not particularly good at finding these objects though -- to help it out use the springCleaning() function.)


Prev: Classes and Inheritance    Next: Inheritance


Last update: May 8, 2024