4 ms·
If you want to declare that the function takes a fixnum and returns a fixnum, you can use a function type declaration: (declaim (ftype (function (fixnum) (va
by bluefox 5y ago
If you want to declare that the function takes a fixnum and returns a fixnum, you can use a function type declaration:
(declaim (ftype (function (fixnum) (values fixnum &optional)) foo))
Of course, if you pass a fixnum and the result cannot actually be stored in a fixnum, that's no good. So you need to either handle that case or not make such a declaration.
- lispm 5y agofor a specific expression on can also specify the return type using THE: (the fixnum (+ a b))
- twoodfin 5y agoThat’s a hilarious but perfect piece of syntax. Do you know where it originated?
- Jtsummers 5y agoPer: https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node106.html https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node106.html > Compatibility note: This construct is borrowed from the Interlisp DECL package; Interlisp, however, allows an implicit progn after the type specifier rather than just a single form. The MacLisp fixnum-identity and flonum-identity constructs can be expressed as (the fixnum x) and (the single-float x).
- vindarel 5y agoagreed^^ there are macros and libraries to bring a nicer syntax (of course). Exple: https://github.com/lisp-maintainers/defstar https://github.com/lisp-maintainers/defstar (defun* (sum -> real) ((a real) (b real)) (+ a b))
- nerdponx 5y agoThere is DECLARE, PROCLAIM, and DECLAIM. I always interpreted the latter to be a portmanteau of the former two.
- bluefox 5y agoRight, though I'm not sure it's easy to infer an ordinary function's return type from this kind of declaration. Speaking of return types, do you have any idea if any implementation takes a generic function's return type declaration seriously? I believe SBCL currently doesn't, which is unfortunate (defmethods keep clobbering the ftype declaration).