4 ms·
Common Lisp allows to annotate code with type information, which can be checked at runtime. Also it's object system works on classes and thus generic function h
by lispm 6y ago
Common Lisp allows to annotate code with type information, which can be checked at runtime. Also it's object system works on classes and thus generic function have arguments for those classes.
Something like Common Lisp is a dynamically typed language, but where implementations can use knowledge about types, either at runtime or even sometimes at compile time.
SBCL
* (defvar *foo*) ; a global variable *FOO*
*FOO*
We tell SBCL that the type of the variable is an integer between 0 and 10.
* (declaim (type (integer 0 10) *foo*))
(*FOO*)
Now we try to set it to 30:
* (setf *foo* 30)
SBCL detects the problem:
; in: SETF FOO
; (SETF FOO 30)
; --> SETQ
; ==>
; (THE (MOD 11) 30)
;
; caught WARNING:
; Constant 30 conflicts with its asserted type (MOD 11).
; See also:
; The SBCL Manual, Node "Handling of Types"
- sevensor 6y agoThat's amazing. I want that. I like how clear it is about what it's for, which is the sort of thing one does all the time in other languages that lack a vocabulary for it. I'm going to assume you could put any predicate you wanted in there?