Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Type annotation for functions in Common Lisp
(annot:defannotation fntype (args result form)
(:arity 3)
(let* ((last-form (cl-annot.util:progn-form-last form))
(symbol (cl-annot.util:definition-form-symbol last-form)))
`(progn (declaim (ftype (function ,args ,result) ,symbol))
,form)))
;; Usage:
;; (ql:quickload :cl-syntax)
;; (syntax:use-syntax :annot)
;;
;; @fntype (fixnum) fixnum
;; (defun fib (n)
;; (if (<= n 1)
;; 1
;; (+ (fib (1- n)) (fib (- n 2)))))
;;
;; ;; OK
;; (defun fib10 () (fib 10))
;;
;; ;; Compilation Warning
;; (defun fibx () (fib 'x))
;;
;; ; caught WARNING:
;; ; Constant X conflicts with its asserted type FIXNUM.
;; ; See also:
;; ; The SBCL Manual, Node "Handling of Types"
;; ;
;; ; compilation unit finished
;; ; caught 1 WARNING condition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.