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.

Note that a trait declaration inherits all method declarations declared by all of its supertraits–there’s no real notion of<br />

overriding, just overloading (as discussed in Chapter 15). A trait declaration provides the method declarations that it<br />

declares or inherits.<br />

<strong>The</strong>re are two sorts of method declarations: dotted method declarations and functional method declarations. Syntactically,<br />

a dotted method declaration is identical to a function declaration, except that a special self parameter is provided<br />

immediately before the name of the method. When a method is invoked, the self parameter is bound to the object<br />

on which it is invoked. If no self parameter is provided explicitly, it is implicitly a parameter with name self . An<br />

explicit self parameter may be an identifier other than self , in which case self is not necessarily declared within<br />

that method.<br />

A functional method declaration does not have a self parameter before the method name. Instead, it has a parameter<br />

named self at an arbitrary position in its parameter list. This parameter is not given a type and implicitly has the<br />

type of the enclosing declaration. Semantically, functional method declarations can be viewed as top-level functions.<br />

For example, the following overloaded functional methods f declared within a trait declaration A:<br />

trait A<br />

f(self, t : T) = e 1<br />

f(s :S,self) = e 2<br />

end<br />

f(a, t)<br />

may be rewritten as top-level functions as follows:<br />

trait A<br />

internalF(t :T) = e 1<br />

internalF(s :S) = e 2<br />

end<br />

f 1 (a :A, t :T) = a.internalF(t)<br />

f 2 (s :S, a :A) = a.internalF(s)<br />

f 1 (a, t)<br />

where internalF is a freshly generated name. Functional method declarations may be overloaded with top-level<br />

function declarations. An abstract function declaration (described in Section 12.3) can be provided also for overloaded<br />

functional method declarations. See Chapter 15 for a discussion of overloaded functionals in <strong>Fortress</strong>.<br />

A non-self self parameter can be used within nested object expressions (described in Section 13.9) to name the outer<br />

object in methods of the inner:<br />

object<br />

m() = object<br />

notSelf .getOuterSelf () = self(∗ “self” declared in outer scope ∗)<br />

getInnerSelf () = self(∗ regular inner “self” ∗)<br />

end<br />

end<br />

When a method declaration includes a body expression, it is called a method definition. A method declaration that<br />

does not have its body expression is referred to as an abstract method declaration. An abstract method declaration<br />

may include the modifier abstract . An abstract method declaration may elide parameter names but parameter types<br />

cannot be omitted except for the self parameter.<br />

Here is an example trait Enzyme which provides methods mass , catalyze , and reactionSpeed :<br />

trait Enzyme extends { OrganicMolecule, Catalyst }<br />

reactionSpeed():Speed<br />

catalyze(reaction) = reaction.accelerate(reactionSpeed())<br />

73

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

Saved successfully!

Ooh no, something went wrong!