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.

9.2 Method Declarations<br />

Syntax:<br />

MdDecl ::= AbsMdDecl<br />

| MdDef<br />

AbsMdDecl ::= [abstract ] MdMod ∗ MdHeader<br />

MdDef ::= MdMod ∗ MdHeader = Expr<br />

| Coercion<br />

MdMod ::= getter | setter | FnMod<br />

MdHeader ::= [(Id | self ) . ]Id [StaticParams]([MdParams])[IsType] FnClauses<br />

MdParams ::= MdParam( , MdParam) ∗<br />

| [MdParam( , MdParam) ∗ , ] Id : TypeRef ...<br />

| [MdParam( , MdParam) ∗ , ] [Id : TypeRef ... , ] MdParam=Expr ( , MdParam=Expr) ∗<br />

MdParam ::= ParamId [IsType]<br />

| self<br />

| TypeRef<br />

ParamId ::= Id<br />

|<br />

IsType ::= : TypeRef<br />

FnClauses ::= [Throws] [Where] [Contract]<br />

Throws ::= throws MayTraitTypes<br />

A trait declaration contains a set of method declarations. Syntactically, a method declaration begins with an optional<br />

sequence of modifiers followed by the method’s name optionally prefixed by a self parameter, optional static parameters<br />

(described in Chapter 11), the value parameter with its (optionally) declared type, an optional type of a return<br />

value, an optional declaration of thrown checked exceptions (discussed in Chapter 14), an optional where clause (discussed<br />

in Section 11.6), an optional contract for the method (discussed in Section 9.4), and finally an optional body<br />

expression preceded by the token = . A throws clause does not include naked type variables. Every element in a<br />

throws clause is a subtype of CheckedException. A trait declaration may contain coercions discussed in Chapter 17.<br />

Method declarations can include the following special modifiers:<br />

getter : A method declaration with the modifier getter explicitly declares a getter method for a field, even in the<br />

absence of an actual field. If such a field exists, there is no implicit getter for the field. An explicitly declared getter<br />

method must take no arguments and return an appropriately typed result. A getter method must not throw any checked<br />

exception. Getter names may not overlap ordinary method names. A getter method must be invoked with the field<br />

access syntax:<br />

expr.id<br />

where id is the name of the getter method.<br />

setter : A method declaration with the modifier setter explicitly declares a setter method for a field, even in<br />

the absence of an actual field. If such a field exists, there is no implicit setter for the field. An explicitly declared<br />

setter method must take a single argument—the value being set—and return () . A setter method must not throw any<br />

checked exception. Setter names may not overlap ordinary method names. A setter method must be invoked with the<br />

assignment syntax:<br />

expr 1 .id := expr 2<br />

We say that a method declaration occurs in a trait declaration. A trait declaration declares a method declaration that<br />

occurs in that trait declaration. A trait declaration inherits method declarations from the declarations of its supertraits.<br />

72

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

Saved successfully!

Ooh no, something went wrong!