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.

FldDef ::= FldMod ∗ Id [IsType] ( = |:= ) Expr<br />

FldMod ::= var | AbsFldMod<br />

Fields are variables local to an object. <strong>The</strong>y must not be referred to outside their enclosing object declarations.<br />

Field declarations in an object declaration are syntactically identical to top-level variable declarations (described in<br />

Section 6.2), with the same meanings attached to the form of variable declarations except that they have a different set<br />

of modifiers.<br />

10.3 Value Objects<br />

An object declaration with the modifier value declares a value object that is called in many languages a primitive<br />

value. <strong>The</strong> object trait type declared by a value object implicitly has the modifier value .<br />

<strong>The</strong> fields of a value object are immutable; they cannot be changed directly or it is a static error. However, <strong>Fortress</strong><br />

allows value objects to have settable fields as an abbreviation for constructing a new value object with a different<br />

value for one field. If a value object has a setter method or a subscripted assignment operator method (described in<br />

Section 34.7), then the return type of the method must be the value object trait type instead of () . When such a method<br />

is invoked, the receiver must itself be assignable, and the value returned by the method is assigned to the receiver.<br />

For example, here is a value object Complex number:<br />

value object Complex(settable real : Double,settable imaginary :Double = 0)<br />

opr +(self,other : Complex) = Complex(real + other.real,imaginary + other.imaginary)<br />

end<br />

When a mutable variable z :<br />

var z : Complex = Complex(0)<br />

updates its imaginary field, the following syntax:<br />

z.imaginary := v<br />

can be used as an abbreviation for:<br />

z := Complex(z.real, v)<br />

So the setter for the field imaginary in Complex would do the work of constructing and returning Complex(z.real, v) ,<br />

and the assignment:<br />

z.imaginary := v<br />

would be construed to mean:<br />

z := z.imaginary(v)<br />

Note that modifying a settable field directly within the value object is not allowed. For example, the following:<br />

imaginary := 3<br />

within the Complex object means:<br />

self.imaginary := 3<br />

and because self is not mutable, the assignment is disallowed.<br />

79

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

Saved successfully!

Ooh no, something went wrong!