User Tools

Site Tools


01_user_documentation:07_rgg_xl:02_xl:08_object:01_module:01_declaration

Declaration

Modules are declared similarly as classes. Their declaration include:

Modules are declared in XL with the keyword module (different from the java keyword module).

module A(int attr1, Object attr2) extends N { /* body */ } ;

Syntax

Only the Name is non optional in the declaration. Thus, the smallest module declaration is:

module A;

This declaration is equivalent to:

class A extends N {
  public static class Pattern extends UserDefinedPattern {
  private static void signature(@In @Out X node) {}
    ... // suitable implementation of abstract methods
  }
}

The declaration uses an implicit superclass N which is determined by the annotation @DefaultModuleSuperclass of the innermost enclosing declaration which has such an annotation. RGG files have default annotations, one of these is the @DefaultModuleSuperclass. Its value is Node. Thus, by default all module extends Node.

The parametrized pattern is created implicitly and takes the module attributes declared after the name as additional arguments.

Attributes declaration

Attributes can be declared in :

  • The list of typed arguments that follow the name
  • As fields directly in the module body

Attributes declared in the the list of typed arguments are used implicitly for both: the constructor, and the pattern.

All attributes implicitly declare their accessors.

Attribute accessors are the methods to get and set the attribute value. The accessors of a Node are automatically resolved by the compiler with the syntax:

Sphere s; // has an attribute diameter
s[diameter] = 5; // automatically resolve the setter
float d = s[diameter]; // automatically resolve the getter
module A (float len); // is equiavent to:
class A extends Node {
 
float len;
 
public A(float l) { len = l; }
 
public static class Pattern extends UserDefinedPattern {
  private static void signature(@In @Out X node,
                                              float len ) {}
  }
}

Using super attribute

Module declarations may also specify their superclass explicitly, use inherited fields in the pattern, and contain an entire class body:

module B ( super.len, String s) extends A; 
 
//is equivalent to (given the previous declaration of A)
class B extends A {
  String s;
  B(float l, String s) {super(l); this.s = s;}
 
  public static class Pattern extends UserDefinedPattern {
    private static void signature(@In @Out Y node,
                                          float a, String b ) {}  
             // Notice how the pattern of B take both a and b as parameters 
  }
}

Superclass constructor

It is also possible to declare the superclass constructor explicitly.

module C ( super.len ) extends B(len, "VAL") ; 
 
// is equivalent to:
class C extends B {
 
  C(float l) {
    super(l, "VAL"); 
  }
 
  ... // pattern
}

In this example the super constructor takes a value “VAL” as an argument, but the argument can be a method declared in the module. I.e.

module C ( super.len ) extends B(len, getName()) {
  String getName() {
    return "MyAmazingName";
  }
}

Add expressions

It is possible to add a list of expression statements to be included in the constructor (using the syntax of with-instance expression lists):

module C ( super.len, super.s ) extends B . ( int i = 0, i++, println(i) ) ; 
 
// is equivalent to:
class C extends B {
 
  C(float l, String s) {
    super(l, s); 
    int i = 0;
    i++;
    println(i);
  }
 
  ... // pattern
}

Super private attributes

To be able to deal with situations where we want to use a field of a superclass as a parameter, but do only have access to this field via get- and set-methods (due to restricted visibility or even because the value is not stored directly in a field, but by some other mechanism), we have to specify the get-method in the declaration of the parameter:

module X { private float a; float getA(){} void setA(float a){} };  // private field
 
module Y(float a return getA()) extends X.(setA(a));
 
//is equivalent to:
class Y extends X {
 
  public Y(float val){
    super();
    setA(val);
  }
 
 ... // pattern
}

As you see the method getA() is not actually used. It only serve as placeholder for the syntax. The method has to be declared though.

01_user_documentation/07_rgg_xl/02_xl/08_object/01_module/01_declaration.txt · Last modified: 2025/09/05 13:08 by gaetan