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.

In both methods cons and append , the self parameter occurs as the second parameter. Calls to these methods look<br />

more like function calls than method calls. For example, in the following call to append , the receiver of the call is l 2 :<br />

append(l 1 , l 2 )<br />

In contrast to singleton object definitions, a dynamically parametric object definition includes a constructor declaration<br />

in its header, as in the following example:<br />

object Particle(position : (R Length) 3 ,<br />

velocity : (R Velocity) 3 )<br />

extends Moving<br />

end<br />

Every call to the constructor of dynamically parametric object Particle yields a new object. For example:<br />

p 1 = Particle([3 m 2 m 5 m], [(1 m/s) 0 0])<br />

<strong>The</strong> parameters to the constructor of a dynamically parametric object implicitly define fields of the object, along with<br />

their getters (by default). <strong>The</strong>se parameters can include all of the modifiers that an ordinary field can. For example, a<br />

parameter can include the modifier hidden , in which case a getter is not implicitly defined for the field.<br />

In the definition of Particle, it is the implicitly defined getters of fields position and velocity that provide concrete<br />

definitions for the inherited abstract methods from trait Moving.<br />

In both singleton and dynamically parametric objects, implicitly defined getters and setters can be overridden by<br />

defining explicit getters and setters, using the modifiers getter and setter . For example, we alter the definition of<br />

Particle to include an explicit getter for field velocity as follows:<br />

object Particle(position : (R Length) 3 ,<br />

velocity : (R Velocity) 3 )<br />

extends Moving<br />

getter velocity() = do<br />

print “velocity getter accessed”<br />

velocity<br />

end<br />

end<br />

<strong>The</strong> explicitly defined getter first prints a message and then returns the value of the field velocity . Note that the final<br />

variable reference in this getter refers directly to the field velocity , not to the getter. Only within an object definition<br />

can fields be accessed directly. In fact, even in an object definition, fields are only accessed directly when they are<br />

referred to as simple variables. In the following definition of Particle, the getter velocity is recursively accessed<br />

through the special variable self (bound to the receiver of the method call):<br />

object Particle(position : (R Length) 3 ,<br />

velocity : (R Velocity) 3 )<br />

extends Moving<br />

getter velocity() = do<br />

print “velocity getter accessed”<br />

self.velocity<br />

end<br />

end<br />

Now, the result of a call to getter velocity is an infinite loop (along with a lot of output).<br />

31

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

Saved successfully!

Ooh no, something went wrong!