19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8<br />

Types<br />

<strong>Fortress</strong> provides several kinds of types: trait types, tuple types, arrow types, BottomType, and other types provided<br />

in the <strong>Fortress</strong> standard libraries. Some types have names. Some types may be parameterized by types and values;<br />

we call these types generic types. Two types are identical if and only if they are the same kind and their names and<br />

arguments (if any) are identical. Types are related by several relationships as described in Section 8.1.<br />

Syntactically, the positions in which a type may legally appear (type context) is determined by the nonterminal TypeRef<br />

in the <strong>Fortress</strong> grammar, defined in Appendix G.<br />

8.1 Relationships between Types<br />

Types in <strong>Fortress</strong> may be related by a subtyping relation, an exclusion relation, or a coercion.<br />

A subtyping relation is reflexive, transitive, and antisymmetric, and is defined by the extends clause of trait declarations.<br />

Every expression has a static type. Every value has a runtime type (dynamic type). <strong>Fortress</strong> programs are<br />

checked before they are executed to ensure that if an expression e evaluates to a value v, the runtime type of v is a<br />

subtype of the static type of e. Sometimes we abuse terminology by saying that an expression has the runtime type of<br />

the value it evaluates to (see Section 4.2 for a discussion about evaluation of expressions). Thus, in the execution of a<br />

valid <strong>Fortress</strong> program, an expression’s runtime type is always a subtype of its static type. We say that a value is an<br />

instance of its runtime type and of every supertype of its runtime type. Every type is a subtype of Object. For types<br />

T and U, we write T ≼ U when T is a subtype of U, and T ≺ U when T ≼ U and T ≠ U.<br />

<strong>Fortress</strong> defines an exclusion relation between types, which relates two disjoint types: no value can have a type that is<br />

a subtype of two types that exclude each other. <strong>The</strong> exclusion relation is irreflexive and symmetric, and is defined by<br />

the excludes and comprises clauses of trait declarations, and what is implied from these by the subtyping relation<br />

(including the fact that object trait types have no strict subtypes, and so exclude all types other than its supertypes).<br />

For example, suppose the following:<br />

trait S comprises {U, V } end<br />

trait T comprises {V, W } end<br />

object U extends S end<br />

object V extends {S, T } end<br />

object W extends T end<br />

Because of the comprises clauses of S and T and the fact that U, V , and W are objects, S and T exclude each<br />

other. We write T ♦ U if T excludes U. If a type excludes another type, it excludes all its subtypes as well:<br />

T ♦ U =⇒ ∀T ′ ≼ T : T ′ ♦ U.<br />

64

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!