| layout | developer-doc | ||
|---|---|---|---|
| title | Dynamic Dispatch | ||
| category | types | ||
| tags |
|
||
| order | 6 |
Enso is a language that supports pervasive dynamic dispatch. This is a big boon for usability, as users can write very flexible code that still plays nicely with the GUI.
The current implementation of Enso supports single dispatch (dispatch purely on
the type of this), but there are broader visions afoot for the final
implementation of dynamic dispatch in Enso.
The actionables for this section include:
- Determining whether we want to support proper multiple dispatch in the future. This is important to know as it has implications for the type system, and the design of the dispatch algorithm.
In order to determine which of the potential dispatch candidates is the correct one to select, the compiler needs to have a notion of specificity, which is effectively an algorithm for determining which candidate is more specific than another.
- Always prefer a member function for both
x.f yandf y xnotations. - Only member functions, current module's functions, and imported functions are
considered to be in scope. Local variable
fcould not be used in thex.f ysyntax. - Selecting the matching function:
- Look up the member function. If it exists, select it.
- If not, find all functions with the matching name in the current module and all directly imported modules. These functions are the candidates.
- Eliminate any candidate
Xfor which there is another candidateYwhosethisargument type is strictly more specific. That is,Ythis type is a substitution ofXthis type but not vice versa. - If not all of the remaining candidates have the same this type, the search fails.
- Eliminate any candidate
Xfor which there is another candidateYwhich type signature is strictly more specific. That is,Ytype signature is a substitution ofXtype signature. - If exactly one candidate remains, select it. Otherwise, the search fails.
The actionables for this section are as follows:
- THE ABOVE VERSION IS OLD. NEEDS UPDATING.
- The definition of specificity for dispatch candidates (including how it interacts with the subsumption relationship on typesets and the ordering of arguments).
It is an open question as to whether we want to support proper multiple dispatch
in Enso. Multiple dispatch refers to the dynamic dispatch target being
determined based not only on the type of the this argument, but the types of
the other arguments to the function.
To do multiple dispatch properly, it is very important to get a rigorous specification of the specificity algorithm. It must account for:
-
The typeset subsumption relationship.
-
The ordering of arguments.
-
How to handle defaulted and lazy arguments.
-
Constraints in types. This means that for two candidates
fandg, being dispatched on a typetwith constraintc, the more specific candidate is the one that explicitly matches the constraints. An example follows:type HasName name : String greet : t -> Nothing in IO greet _ = print "I have no name!" greet : (t : HasName) -> Nothing in IO greet t = print 'Hi, my name is `t.name`!' type Person Pers (name : String) main = p1 = Person.Pers "Joe" greet p1 # Hi, my name is Joe! greet 7 # I have no name
Here, because
Personconforms to theHasNameinterface, the secondgreetimplementation is chosen because the constraints make it more specific.