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


5.3.3 Global Function Type Inference

As described in section function-types, a global function type (ftype) declaration places implicit type assertions on the call arguments, and also guarantees the type of the return value. So wherever a call to a declared function appears, there is no doubt as to the types of the arguments and return value. Furthermore, Python will infer a function type from the function’s definition if there is no ftype declaration. Any type declarations on the argument variables are used as the argument types in the derived function type, and the compiler’s best guess for the result type of the function is used as the result type in the derived function type.

This method of deriving function types from the definition implicitly assumes that functions won’t be redefined at run-time. Consider this example:

(defun foo-p (x)
  (let ((res (and (consp x) (eq (car x) 'foo))))
    (format t "It is ~:[not ~;~]foo." res)))

(defun frob (it)
  (if (foo-p it)
      (setf (cadr it) 'yow!)
      (1+ it)))

Presumably, the programmer really meant to return res from foo-p, but he seems to have forgotten. When he tries to call do (frob (list 'foo nil)), frob will flame out when it tries to add to a cons. Realizing his error, he fixes foo-p and recompiles it. But when he retries his test case, he is baffled because the error is still there. What happened in this example is that Python proved that the result of foo-p is null, and then proceeded to optimize away the setf in frob.

Fortunately, in this example, the error is detected at compile time due to notes about unreachable code (see dead-code-notes.) Still, some users may not want to worry about this sort of problem during incremental development, so there is a variable to control deriving function types.

Variable: extensions:*derive-function-types*

If true (the default), argument and result type information derived from compilation of defuns is used when compiling calls to that function. If false, only information from ftype proclamations will be used.


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