Subject: Re: static, dynamic and implicitely typed languages
From: (Rob Warnock)
Date: Mon, 13 Oct 2008 04:13:18 -0500
Newsgroups: comp.lang.lisp
Message-ID: <>
Chris Barts  <> wrote:
| You can also explicitly declare the types of variables,
| as in this function:
| (defun foo (a b c)
|    (declare (type number a)
|             (type fixnum b)
|             (type cons c))
|    (cons (+ a (* 2 b)) c))
| The compiler obviously knows that the function must return a list, but
| it *also* knows that it can multiply B by 2 really quickly because a
| FIXNUM is a small number, whereas the addition (+ C (* 2 B)) will
| likely be slower because C can be any kind of number.

To really get the maximum speed, you have to be able to assure the
compiler that the *result* of multiplying B by 2 will also still be
a FIXNUM. If you *are* sure of that, then you can tell a CL compiler
about it this way:

  (defun foo (a b c)
     (declare (type number a)
              (type fixnum b)
              (type cons c))
     (cons (+ a (the fixnum (* 2 b))) c))

As you note, this may not be of much benefit, since the compiler
will still have to use fully-generic "+" when adding "A"...  :-{


Rob Warnock			<>
627 26th Avenue			<URL:>
San Mateo, CA 94403		(650)572-2607