4 ms·
I've said it before, but I'll say it again. Common Lisp already features strong static typing (the example below uses SBCL): * (defun foo (x) x) FOO *
by xenophonf 12y ago
I've said it before, but I'll say it again. Common Lisp already features strong static typing (the example below uses SBCL):
* (defun foo (x) x)
FOO
* (declaim (ftype (function (fixnum)) foo))
* (defun bar (y) (declare (string y)) (foo y))
; in: DEFUN BAR
; (FOO Y)
;
; caught WARNING:
; Derived type of Y is
; (VALUES STRING &OPTIONAL),
; conflicting with its asserted type
; FIXNUM.
; See also:
; The SBCL Manual, Node "Handling of Types"
;
; compilation unit finished
; caught 1 WARNING condition
BAR
One could also declare the function signature first (perhaps in skeleton code generated by modelling tools). Then, if someone implements it incorrectly, the compiler will throw an error (again, SBCL):
* (declaim (ftype (function (fixnum)) qux))
* (defun qux (z) (car z))
; in: DEFUN QUX
; (CAR Z)
;
; caught WARNING:
; Derived type of Z is
; (VALUES FIXNUM &OPTIONAL),
; conflicting with its asserted type
; LIST.
; See also:
; The SBCL Manual, Node "Handling of Types"
;
; compilation unit finished
; caught 1 WARNING condition
QUX
Here's a good discussion of static typing in Common Lisp:
http://www.lispforum.com/viewtopic.php?f=2&t=191 http://www.lispforum.com/viewtopic.php?f=2&t=191
TL;DR is that many CL implementations will check types at compile time, but for appropriate speed and safety settings, they will also generate code that checks types at runtime, too.
- tempodox 12y agoThis is, of course, correct and I do use that technique in SBCL myself, but it still doesn't make SBCL a statically typed language (even if SBCL has the strictest type checks and the best native code generation I've seen in an open-source Lisp to date). The static typing in OCaml, Haskell - heck, even C is still another thing.
- xenophonf 12y agoI don't know OCaml or Haskell, but I think you're mistaken about C. Given the following C code: char *foo(char *bar) { return bar; } int main(int argc, char *argv[]) { foo(1); return 0; } I get compile-time warnings from clang substantially similar to what SBCL emits: $ cc test.c test.c:6:7: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'char *' [-Wint-conversion] foo(1); ^ test.c:1:17: note: passing argument to parameter 'bar' here char *foo(char *bar) { ^ 1 warning generated. I'm sure that with suitable flags set, both compilers would throw type errors at compile-time instead of warnings.