Next: , Previous: , Up: Type Inference   [Contents][Index]


5.3.2 Local Function Type Inference

The types of arguments to local functions are inferred in the same was as any other local variable; the type is the union of the argument types across all the calls to the function, intersected with the declared type. If there are any assignments to the argument variables, the type of the assigned value is unioned in as well.

The result type of a local function is computed in a special way that takes tail recursion (see tail-recursion) into consideration. The result type is the union of all possible return values that aren’t tail-recursive calls. For example, Python will infer that the result type of this function is integer:

(defun ! (n res)
  (declare (integer n res))
  (if (zerop n)
      res
      (! (1- n) (* n res))))

Although this is a rather obvious result, it becomes somewhat less trivial in the presence of mutual tail recursion of multiple functions. Local function result type inference interacts with the mechanisms for ensuring proper tail recursion mentioned in section local-call-return.


Next: Global Function Type Inference, Previous: Variable Type Inference, Up: Type Inference   [Contents][Index]