4 ms·
Your notion of 'type-correctness' seems fairly static and you don't know Common Lisp either. A type hint in Common Lisp is the same as a type declaration. Commo
by jgrant27 17y ago
Your notion of 'type-correctness' seems fairly static and you don't know Common Lisp either. A type hint in Common Lisp is the same as a type declaration. Common Lisp just let's you declare the types optionally.
If you declare a type for a value and then try to store a value of an incorrect type then SBCL(and many other CL compilers) will respond with a compiliation error(not a warning) :
; in: LAMBDA NIL
; (TYPE (SINGLE-FLOAT NODE))
;
; caught ERROR:
; Bound is not *, a SINGLE-FLOAT or a list of a SINGLE-FLOAT: NODE
;
; compilation unit finished
; caught 1 ERROR condition
debugger invoked on a SB-INT:COMPILED-PROGRAM-ERROR in thread #<THREAD "initial thread" RUNNING {1002A5A051}>:
Execution of a form compiled with errors.
Form:
#'(NAMED-LAMBDA TRAVERSE-PRE-ORDER (NODE FUN)
(DECLARE (TYPE (SINGLE-FLOAT NODE))
(IF NODE
(PROGN
(APPLY FUN (LIST NODE))
(IF (BTNODE-LEFT NODE)
(TRAVERSE-PRE-ORDER (BTNODE-LEFT NODE) FUN))
(IF (BTNODE-RIGHT NODE)
(TRAVERSE-PRE-ORDER (BTNODE-RIGHT NODE) FUN)))))
(BLOCK TRAVERSE-PRE-ORDER))
Compile-time error:
Bound is not *, a SINGLE-FLOAT or a list of a SINGLE-FLOAT: NODE
Type HELP for debugger help, or (SB-EXT:QUIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [CONTINUE] Ignore runtime option --load "tree-traversal.lisp".
1: [ABORT ] Skip rest of --eval and --load options.
2: Skip to toplevel READ/EVAL/PRINT loop.
3: [QUIT ] Quit SBCL (calling #'QUIT, killing the process).
((LAMBDA ()))
- prodigal_erik 17y agoCan you get a compilation error for (defun foo (x) (car x)) because x is not known to be a list? I've never seen a lisp do that, though at times I wanted it.
- jgrant27 17y ago(defun foo (x) (declare (type list x)) (car x))
- gwern 17y agoSorry, still just a warning: $ sbcl This is SBCL 1.0.18.debian, an implementation of ANSI Common Lisp. More information about SBCL is available at <http://www.sbcl.org/> http://www.sbcl.org/>. SBCL is free software, provided as is, with absolutely no warranty. It is mostly in the public domain; some portions are provided under BSD-style licenses. See the CREDITS and COPYING files in the distribution for more information. * (defun foo (x) (declare (type array x)) (car x)) ; in: LAMBDA NIL ; (CAR X) ; ; note: deleting unreachable code ; ; caught WARNING: ; Asserted type LIST conflicts with derived type (VALUES ARRAY &OPTIONAL). ; See also: ; The SBCL Manual, Node "Handling of Types" ; ; compilation unit finished ; caught 1 WARNING condition ; printed 1 note FOO
- jgrant27 17y agoFirst of all, you have modified the function that I posted (you changed the type declaration from a list to an array). Second, you do not read your compiler warning messages. SBCL is trying to tell you that you shouldn't type something as an array when you are clearly attempting to use it as a list e.g. calling the CAR function on an array makes no sense. A list and an array in Lisp are not the same. The CAR function in Lisp only works on lists. * (defun foo (x) (declare (type array x)) (car x)) ; in: LAMBDA NIL ; (CAR X) ; ; caught WARNING: ; Asserted type LIST conflicts with derived type (VALUES ARRAY &OPTIONAL). ; See also: ; The SBCL Manual, Node "Handling of Types" It's correctly warning you before you even try and call the foo function, because it will fail no matter what you pass to it !