19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Syntactically, an object declaration begins with an optional sequence of modifiers followed by the special reserved<br />

word object , followed by the identifier of the object, optional static parameters (described in Chapter 11), optional<br />

value parameters, optional traits the object extends, an optional declaration of thrown checked exceptions (discussed<br />

in Chapter 14), an optional where clause (discussed in Section 11.6), an optional contract for the object (discussed in<br />

Section 12.4), a list of method declarations, field declarations, and property declarations (described in Section 19.6),<br />

and finally the special reserved word end . If an object declaration has no extends clause, the object implicitly<br />

extends only trait Object. A throws clause does not include naked type variables. Every element in a throws<br />

clause is a subtype of CheckedException. If an object declaration has a contract, the contract is evaluated as function<br />

contracts (described in Section 12.4) when the object is created.<br />

<strong>The</strong>re are two kinds of object declarations: singleton object declarations and parametric object declarations. A singleton<br />

object declaration does not have any static or value parameter; it declares a sole, stand-alone object. <strong>The</strong>re are two<br />

kinds of parametric object declarations: statically parametric objects and dynamically parametric objects. Statically<br />

parametric objects are parameterized by static parameters and dynamically parametric objects are parameterized by<br />

value parameters (possibly with static parameters). A dynamically parametric object declaration includes a constructor<br />

declaration and every call to the constructor of such an object with the same argument yields a new object. A statically<br />

parametric object declaration does not include a constructor declaration and every instantiation of such an object with<br />

the same argument yields the same singleton object. Initialization of parametric objects is entirely demand-driven as<br />

described in Section 22.6.<br />

Each value parameter of a parameterized object declaration may be preceded by a sequence of field modifiers or the<br />

special modifier transient : A value parameter preceded by the modifier transient doesn’t correspond to a field<br />

in an instantiation of the object. transient parameters are not in scope of the object’s method declarations.<br />

Fields can be also explicitly defined within a parameterized object declaration as within a singleton object declaration.<br />

All fields of an object are initialized before that object is made available to subsequent computations. Syntactically,<br />

method declarations in object declarations are identical to method declarations in trait declarations.<br />

For example, the following empty list object extending trait List:<br />

object Empty extends { List }<br />

first() = throw Error<br />

rest() = throw Error<br />

cons(x) = Cons(x,self)<br />

append(xs) = xs<br />

end<br />

has no fields and four methods.<br />

Here is an example of a parameterized Cons object extending trait ListT :<br />

object ConsT(first : T,rest : ListT)<br />

extends ListT<br />

cons(x) = Cons(x,self)<br />

append(xs) = Cons(first,rest.append(xs))<br />

end<br />

Note that this declaration implicitly introduces the “factory” function ConsT(first : T,rest : ListT) :ConsT<br />

which is used in the body of the object declaration to define the cons and append methods. Multiple factory functions<br />

can be defined by overloading a parametric object with functions. For example: Cons(first : T) = Cons(first, Empty) .<br />

10.2 Field Declarations<br />

Syntax:<br />

78

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

Saved successfully!

Ooh no, something went wrong!