Types and Classes

Sather - Language Specification
Prev |  | Next

# Chapter 3. Types and Classes

Sather programs are textually made up of classes. Classes are used to define the code and storage that make up types. Each object is an instance of a type. Types can be thought of as representing sets of objects at runtime. Objects never change their type.

There are four kinds of objects in Sather: immutable (e.g. integers), reference (e.g. strings), closures, and external (used to represent entities in other languages). There are four corresponding concrete types: immutable, reference, closure, and external types. There are also abstract types, which represent sets of concrete types. Immutable, reference, external, and abstract types are defined textually by immutable, reference, external, and abstract classes. Partial classes define code that does not have corresponding objects or types, and may be included by other classes to obtain implementation.

The type graph for a program is a directed acyclic graph that is constructed from the program's source text. Its nodes are types and its edges represent the subtype relationship. If there is a path in this graph from a type t1 to a type t2, we say that t2 is a subtype of t1 and that t1 is a supertype of t2. Subtyping is reflexive; any type is a subtype of itself. Only abstract types and method closures can be supertypes (see pages See Abstract classes and See Closure creation expressions); closure types can only be supertypes of other closure types.

Every Sather variable has a declared type. The fundamental typing rule is: An object can only be held by a variable if the object's type is a subtype of the variable's type. It is not possible for a program which compiles to violate this rule (i.e. Sather is statically type-safe).

# 3.1. Type specifiers

Example 3-1. Examples:

    INT
    A{B,C{$D}}
    $IS_EQ{T}
    ROUT{A,B,C}:D
    ITER{INT}:INT
    SAME

    type_specifier ==>
            ( uppercase_identifier | abstract_class_name ) [ { type_specifier_list } ] | method_closure_type_specifier | SAME

    method_closure_type_specifier ==>
            routine_closure_type_specifier | iter_closure_type_specifier

    routine_closure_type_specifier ==>
            ROUT [ {  routine_mode type_specifier { ,  routine_mode type_specifier } } ] [ : type_specifier ]

    iter_closure_type_specifier ==>
            ITER [ {  iter_mode type_specifier { ,  iter_mode type_specifier } } ] [ : type_specifier ]

In source text, Sather types are specified by one of the following forms of type specifier:

- The name of a class or abstract class (e.g. 'A' or '$A'). This may be followed by a list of parameter type specifiers in braces (e.g. 'A{B,C}'). The parameter values must not cause the generation of an infinite number of types (e.g. 'FOO{FOO{T}}' within the class 'FOO{T}').

- The name of a type parameter within the body of a parameterized class or abstract type definition (e.g. 'T' in the body of 'class B{T} is ... end').

- The keyword 'ROUT' or 'ITER' optionally followed by a list of argument types in braces, optionally followed by a colon and return type (e.g. 'ROUT{A,B}:C', 'ITER{A,B}:C'). This is used for closure types (See Closures).

- The special type specifier 'SAME,' which denotes the type of the class in which it occurs.

Prev | Home | Next
Lexical Structure | Up | Signatures