Shared attribute definitions

Sather - Language Specification
Prev | Chapter 4. Class Elements | Next

# 4.2. Shared attribute definitions

Example 4-2. Examples:

    private shared i,j: INT;
    shared s:STR := "name";
    readonly shared c:CHAR := 'x';

    shared_definition ==>
            [ private | readonly ] shared ( identifier : type_specifier := expression |  identifier_list : type_specifier )

Shared attributes are global variables that reside in a class namespace. When only a single shared attribute is defined, a constant initializing expression may be provided (See Constant definitions). If no initializing expression is provided, the shared is initialized to the value 'void' (See void expressions).

Each shared definition causes the definition of a reader routine and a writer routine, both with the same name. The reader routine takes no arguments and returns the value of the shared. Its return type is the shared's type. The reader routine is private if the shared is declared 'private'. The writer routine sets the value of the shared, taking a single argument whose type is the shared's type, and has no return value. The writer routine is private if the shared is declared either 'private' or 'readonly'.

> NOTE: When a class containing shared attributes is included in other classes, the including classes get their own, independant copy of the shared attribute. The parent's version of the shared attribute may be accessed using the :: notation.

Prev | Home | Next
Class Elements | Up | Attribute definitions