Aspects - brombres/Rogue GitHub Wiki

Syntax

class AspectName : BaseAspect, ... [aspect]
  PROPERTIES
    ...
  METHODS
    method alpha
      ...

    method beta [insert]
      ...

    method gamma [append]
      ...
 endClass

 class ClassName : BaseClass, AspectName, AspectName, ...
   ...

Description

Aspects are vaguely similar to Java interfaces. Unlike Java interfaces, aspects can contain properties and full method definitions.

Aspects are said to be incorporated into standard classes. When aspects are incorporated, their code is merged with that of the incorporating class.

When class C extends base class B and also incorporates aspects X and Y (class C : B, X, Y), properties from all four types are merged. When properties share the same name but have different initial values, the version that extended class C ends up with uses the following order of precedence based on the order in which the base types were given (B, X, Y):

  1. As defined in class C (first choice).
  2. As defined in aspect Y.
  3. As defined in aspect X.
  4. As defined in base class B (last choice).

The base class and then each aspect can be thought of as each overlaying a new definition of a given property, with the extended class having the final say.

Methods are merged the same way but they have additional options as well. Aspect methods can have the attribute [insert] or [append]. After methods are otherwise inherited, incorporated, and overridden, any [insert] and [append] methods have their code inserted in or appended to C's overridden version.

Note that [insert] and [append] merge into override methods but simply replace inherited methods.

Aspects cannot be instantiated, but reference variables of aspect types may be used to reference any object that incorporates that aspect and to call any method defined by that aspect.

Example

Cat("Fluffy").speak  # "Mr. Fluffy meows!"
println

class Pet( name:String )
  METHODS
    method speak
      print name " makes a noise"
endClass

class Cat : Mr Excited Meowing Pet
  METHODS
endClass

class Meowing [aspect]
  PROPERTIES
    name : String

  METHODS
    method speak
      print name " meows"
endClass

class Mr [aspect]
  METHODS
    method speak [insert]
      print "Mr. "
endClass

class Excited [aspect]
  METHODS
    method speak [append]
      print "!"
endClass