Lunar Programming Language

by David A. Moon
January 2017 - January 2018



Generic Methods

A generic method represents a potentially infinite set of methods that are generated by supplying generic actual parameters for the method's generic formal parameters.

Generic formal parameters are in scope in regular formal parameter types and default expressions as well as in the method body.

Unlike generic formal parameters of a class, generic formal parameters of a method do not have types or defaults.

Invoking a function that has a generic method computes generic actual parameters from the regular actual parameters of the call as follows and adds the generated method to the set of applicable methods, if it is applicable.

1. If a generic formal parameter has the same name as a regular formal parameter, the generic actual parameter is the corresponding regular actual parameter.

2. If a generic formal parameter is used as the type of a regular formal parameter, the generic actual parameter is the exact class of the corresponding regular actual parameter. (But see below.)

3. If a generic formal parameter is used as a generic actual parameter of a generic class in the type of a regular formal parameter of the method, the generic actual parameter of the method is the generic actual parameter of the class of the corresponding regular actual parameter.

If a generic formal parameter appears more than once in a generic method's regular formal parameter list, all computed generic actual parameters for that generic formal parameter must be equal, with one exception: When a generic formal parameter is the type of a regular formal parameter, the generic actual parameter can be generalized to a less specific type to match other occurrences of the same generic formal parameter, provided that the regular actual parameter is still a member of that type.

A generic formal parameter cannot appear anywhere else in a generic method's formal parameter list, except in a default expression, where it does not contribute to computing the generic actual parameter.

When there is more than one generic method in a function that could generate an instantiation applicable to the given actual parameters, only the most specific generic method is used. The specificity rules for generic methods are similar to those for regular methods.

Syntax

A generic method uses the same defining syntax as a regular method, but preceded by a list of generic formal parameter names enclosed in [ ] brackets and separated by commas.

For example,

def[S, T] func(x S, y sequence[S], z T) ...body...
has S and T as generic formal parameters and has x, y, and z as regular formal parameters. In this example, invoking func with three actual parameters and a sequence as the second actual parameter, will set S to the member type of the sequence and will set T to the class of the third actual parameter. The method will only be applicable if the class of the first actual parameter is <= S.

It is intentional that generic class definitions place the generic formal parameter list after the class name, while generic method definitions place the generic formal parameter list before the function name. A generic class name can be "invoked" with generic actual parameters and the result is a class, but a generic method's function name cannot be "invoked" with generic actual parameters. Instead generic methods are "instantiated" automatically during Method Selection.


Previous page   Table of Contents   Next page



Creative Commons License
Lunar by David A. Moon is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Please inform me if you find this useful, or use any of the ideas embedded in it.
Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.