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.

Chapter 17<br />

Conversions and Coercions<br />

<strong>Fortress</strong> provides a mechanism, called coercion, to allow a value of one type to be automatically converted to a value of<br />

another type. Programmers can define coercions (described in Section 17.2) and coercions may change the set of applicable<br />

declarations for a call as described in Section 17.4. If multiple coercions can be applied to a particular functional<br />

call then the most appropriate coercion is chosen statically. Restrictions on coercion declarations (described in Section<br />

17.6) guarantee that the static resolution of coercion (described in Section 17.5) is well-defined. <strong>Fortress</strong> provides<br />

implicit coercions for tuple types and arrow types (described in Section 17.7). <strong>Fortress</strong> also provides an additional<br />

feature of coercion that allows “widest-need” evaluation of numerical expressions (described in Section 17.8).<br />

17.1 Principles of Coercion<br />

In certain situations it is convenient to be able to use a value of one type T as if it were a value of another type U<br />

even though T is not a subtype of U . For example, it is convenient to be able to use the integer-valued expression<br />

2 in a floating-point expression even though its type is not a subtype of any floating-point type. <strong>Fortress</strong> supports the<br />

automatic conversion of integer values to floating-point values (the technical term for this kind of automatic conversion<br />

is coercion). In this way one can write (x + 1)/2 rather than (x + 1.0)/2.0 , for example. Such coercion applies<br />

generally to function and method (collectively called as functional) calls as well as to operators; one can write ln 2<br />

rather than ln 2.0 , or arctan(1, 1) rather than arctan(1.0, 1.0) , for example.<br />

One way to think about coercion is that if type T can be coerced to type U , then a value of type T can be used to<br />

“stand in” for a value of type U in a functional call. This is different from actually being of type U ; it means only<br />

that, given any value of type T , an appropriate value of type U can be computed to substitute for it. Also, coercion<br />

occurs only when the declared type of the corresponding parameter in the functional declaration is exactly the type U<br />

being coerced to (in whose declaration the coercion is defined) not if it is a supertype of U . Note, however, that if<br />

type T can be coerced to type U then any subtype of T can also be coerced to type U .<br />

Coercion from T to U may also occur in variable definitions and assignment expressions when the declared type of<br />

the left-hand side is U and the type of the expression on the right-hand side is a subtype of T .<br />

Coercion is not automatically chained in <strong>Fortress</strong>, unlike some other programming languages: even if type T can be<br />

coerced to type U and type U can be coerced to type V , it is not necessarily the case that type T can be coerced<br />

to type V . Type T can be coerced to type V only if trait V itself contains an appropriate coercion declaration to<br />

handle that particular type coercion.<br />

Example 1: For any floating-point parameter, a decimal integer literal argument may be used.<br />

Example 2: For any floating-point parameter, a floating-point argument of a shorter format may be used.<br />

137

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

Saved successfully!

Ooh no, something went wrong!