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.

Example 3: For any floating-point interval parameter, a non-interval floating-point argument (of the same or shorter<br />

floating-point format) may be used.<br />

17.2 Coercion Declarations<br />

Syntax:<br />

Coercion ::= coercion [StaticParams](Id IsType)CoercionClauses = Expr<br />

CoercionClauses ::= [Throws] [CoercionWhere] [Contract]<br />

CoercionWhere ::= where { CoercionWhereClauseList }<br />

CoercionWhereClauseList ::= CoercionWhereClause ( , CoercionWhereClause) ∗<br />

CoercionWhereClause ::= WhereClause<br />

| TypeRef widens or coerces TypeRef<br />

To declare that trait U allows a coercion from type T , the declaration of trait U must provide a coercion declaration<br />

whose parameter type is T . Coercion declarations are like functional declarations, except that coercion is actually<br />

a special reserved word, a coercion declaration may have special where -clause constraints (described below), and it is<br />

not permitted to specify a return type, because the return type must be the very trait in whose declaration the coercion<br />

declaration appears. <strong>The</strong> coercion body is required; there is no such thing as an abstract coercion declaration.<br />

Coercions may have static parameters (described in Chapter 11) and a where clause (described in Section 11.6), just<br />

like functionals. <strong>The</strong> where clause may contain one of the following special constraints:<br />

Type 1 coerces Type 2<br />

Type 1 widens Type 2<br />

Type 1 widens or coerces Type 2<br />

<strong>The</strong> first is true if trait Type 1 has a coercion from Type 2 . <strong>The</strong> second is true if trait Type 1 has a widening coercion<br />

(described in Section 17.8) from Type 2 . <strong>The</strong> third may be used only in a coercion with the “widening ” special<br />

reserved word; it is true if Type 1 has a coercion from Type 2 , and furthermore the coercion declaration to which the<br />

where clause belongs is widening only if Type 1 has a widening coercion from Type 2 . For example:<br />

trait VectorT extends Number<br />

widening coercion U extends Number(x :VectorU)<br />

where { T widens or coerces U } = . . .<br />

end<br />

Coercions, unlike methods, are not inherited. If trait V extends trait U , and trait U has a coercion from type T , then<br />

V does not thereby have a coercion from type T . (It may, however, have its own coercion from type T , separately<br />

defined within the body of V .) For example, given the following declarations,<br />

trait A<br />

coercion (b :B) = . . .<br />

end<br />

object B end<br />

trait C extends A end<br />

f(c :C) = 5<br />

a call to f(B) is considered a static error because trait C does not inherit a coercion from type B .<br />

138

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

Saved successfully!

Ooh no, something went wrong!