Skip to content

Instantly share code, notes, and snippets.

@fukamachi
Created June 24, 2018 15:07
Show Gist options
  • Save fukamachi/789ee126c1867ea648e1b7050b935fa9 to your computer and use it in GitHub Desktop.
Save fukamachi/789ee126c1867ea648e1b7050b935fa9 to your computer and use it in GitHub Desktop.
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