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.

Chapter 15<br />

Overloading and Multiple Dispatch<br />

<strong>Fortress</strong> allows functions and methods (collectively called as functionals) to be overloaded; that is, there may be<br />

multiple declarations for the same functional name visible in a single scope (which may include inherited method<br />

declarations), and several of them may be applicable to any particular functional call.<br />

Calls to overloaded functionals are checked via a preliminary static dispatch on the static type of the argument, followed<br />

by dynamic dispatch on the runtime type of the argument. <strong>Fortress</strong> imposes restrictions (described in Chapter<br />

33) on overloaded functional declarations that ensure there exists a unique most specific declaration for every call.<br />

Thus, it is unambiguous at run time which declaration should be applied at run time.<br />

In this chapter, we describe how to determine which declarations are applicable to a particular functional call, and<br />

when several are applicable, how to select among them. Section 15.1 introduces some terminology and notation. In<br />

Section 15.2, we show how to determine which declarations are applicable to a named functional call (a function<br />

call described in Section 13.6 or a naked method invocation described in Section 13.5) when all declarations have<br />

only ordinary parameters (without varargs or keyword parameters). We discuss how to handle dotted method calls<br />

(described in Section 13.4) in Section 15.3, and declarations with varargs and keyword parameters in Section 15.4.<br />

Determining which declaration is applied, if several are applicable, is discussed in Section 15.5.<br />

15.1 Terminology and Notation<br />

When there are two or more function declarations of the same name within a single lexical scope, we say that the<br />

function name is overloaded within that lexical scope; we also say that each of the function declarations is overloaded,<br />

and that any pair of the function declarations are mutually overloaded. Top-level function declarations in a component<br />

are permitted to be overloaded with function declarations imported from APIs (using import functionNames from<br />

apiName ). Likewise, it is permitted to have two or more method declarations (declared or inherited) of the same<br />

method name within a single trait or object declaration; we say that the method name is overloaded within that trait or<br />

object declaration, and we also say that each of the method declarations is overloaded, and that any pair of the method<br />

declarations are mutually overloaded.<br />

We assume throughout this chapter that all static variables have been instantiated or inferred. Although there may<br />

be multiple declarations with the same functional name, it is a static error for their static parameters to differ (up to<br />

α-equivalence), or for one declaration to have static parameters and another to not have them. Hence, static parameters<br />

do not enter into the determination of which declarations are applicable, so we ignore them for most of this chapter.<br />

Recall from Chapter 8 that we write T ≼ U when T is a subtype of U, and T ≺ U when T ≼ U and T ≠ U.<br />

121

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

Saved successfully!

Ooh no, something went wrong!