Classes

Simple classes are available in FPP. Those classes work as structures and can be used as any other local variable in the script. Due to the code path dependent nature of classes, their fields are not precomputable.

// Class definition
class MyObject {
    def aFloat = 1.25 // Declare and initialize a float field
    def aDate = "2010-01-01" // Declare and initialize a date field
    def aBoolean = true // Declare and initialize a boolean field
    def aString = "Hello world" // Declare and initialize a string field
}

Arrays Inside Classes

Example of arrays inside structure:

class Item {
    /* 
    * Declare array field using new 
    * array(COUNT, fillValue) 
    * syntax
    */
    def someDoubleArray = array(3, 10)
    def someStringArray = array(1, "toto")
    def someDateArray = array(4, "2010-11-01")
    def someBooleanArray = array(2, true)
 
    /* 
    * Declare array field using new 
    * array("type") syntax
    * This syntax works only inside Item. 
    * It allocates a non-resizable array 
    * of size 0 (to be reassigned).
    */
    def someDoubleArray2 = array("double")
    def someStringArray2 = array("string")
    def someDateArray2 = array("date")
    def someBooleanArray2 = array("boolean")
}

Constructors

To create an instance of the class, use its name as a function call. It initializes fields with values provided in the class body. You will then be able to edit the fields one by one like in the following example:

x = MyObject()
x.aFloat = 42
trace x.aFloat // will print 42

Class Methods

Classes can have methods as in the following code:

class S {
    def a = 0
 
    def get() {
        return a
    }
 
    def get(factor) {
        return a * factor
    }
 
    def set(value){
        a = value
    }
}
 
s = S(1)
// ...
return s.get()

Methods are declared just as script functions, but inside a class body.

Methods are used exactly as functions, except that they are called on objects. Methods accepts same types as parameter/return value as script functions.

Methods support overloading. You can have a method with the same name as another method, assuming they have different parameter number.

Methods can access object fields.

Parameter scope hides field scope, which in turn hides document variable scope.

Class Visibility

By default, class visibility is restricted. However, you control the visibility of a class with annotations:

  • default: if no annotation is present, the class can only be used in its module.
  • @Protected: the class is visible in its whole library.
  • @Public: the class is visible everywhere.
class S {} //class visible only in the module
  
@Protected
class T {} //class visible only in the library
   
@Public
class U {} //class visible everywhere