General Guide to Common Lisp Types

This question may be too general, but I will try: Is there any comprehensive guide to the generic lisp types?

I am a little confused about this question:

Why aren't the primitive types declared in make-array :element-typeadvance to t? Is there any way to check the runtime or runtime of a real declared type?

Why are certain types of CLOS slots not working as limitations, allowing you to put any type of value in the slot? Again, what about checks?

The same goes for declaring function types with declare.. Are they just optimization hints for the compiler?

Also, can I use special type specifiers, including satisfiesin predefined places for some reliable checks, or can they be used only for explicit checks with typepetc?

As you can see, I have some kind of inconvenience in my head, so I really appreciate any neat guide (or set of guides).

I am on SBCL, but I will also be happy to know about the differences between implementations.

+4
source share
3 answers

You must tell the compiler to optimize for security if you want it to actually use types:

CL-USER> (declaim (optimize (safety 3)))
NIL
CL-USER> (defclass foobar () ())
#<STANDARD-CLASS COMMON-LISP-USER::FOOBAR>
CL-USER> (defun foo (a)
           (make-array 1 :element-type 'foobar
                         :initial-contents (list a)))
FOO
CL-USER> (foo (make-instance 'foobar))
#(#<FOOBAR {1005696CE3}>)
CL-USER> (foo 12)
;=> ERROR
CL-USER> (declaim (ftype (function (integer integer) integer) quux))
(QUUX)
CL-USER> (defun quux (a b)
           (+ a b))
QUUX
CL-USER> (quux 12 12)
24 (5 bits, #x18, #o30, #b11000)
CL-USER> (quux 12 "asd")
;=> ERROR

( ) , .

(declaim (optimize (safety 3)))

(defun some-predicate-p (a)
  (format t "~&Checking type...")
  (integerp a))

(deftype foo () '(satisfies some-predicate-p))

(defclass bar ()
  ((foo :type foo :initarg :foo)))

(declaim (ftype (function (foo) list) qwerty))
(defun qwerty (foo)
  (loop repeat 10 collecting (make-instance 'bar :foo foo)))

(qwerty 12)
; Checking type...
; Checking type...
; Checking type...
; Checking type...
; Checking type...
; Checking type...
; Checking type...
; Checking type...
; Checking type...
; Checking type...
; Checking type...
;=> (#<BAR {1003BCA213}> #<BAR {1003BCA263}> #<BAR {1003BCA2B3}>
;    #<BAR {1003BCA303}> #<BAR {1003BCA353}> #<BAR {1003BCA3A3}>
;    #<BAR {1003BCA3F3}> #<BAR {1003BCA443}> #<BAR {1003BCA493}>
;    #<BAR {1003BCA4E3}>)

, , , CHECK-TYPE .

+5

make- : element-type t? ?

:element-type , - . . Lisp , , , .

CLOS- , ? , ?

.

Clozure CL:

? (defclass foo () ((bar :type integer :initform 0 :initarg :bar)))
#<STANDARD-CLASS FOO>
? (make-instance 'foo :bar "baz")
> Error: The value "baz", derived from the initarg :BAR,
  can not be used to set the value of the slot BAR in
  #<FOO #x302000D3EC3D>, because it is not of type INTEGER. 

. ?

- , Symbolics Genera . . , , , - - , / . (, , ,...)

, , CMUCL (SBCL,...), .

ANSI CL. .

+4

, , . SBCL , .

, n s, , n - . , , n , s . s, , s . , , , , , . , , n .

, , , , , , ( ). , .

: , , , CL:

http://sellout.imtqy.com/2012/03/03/common-lisp-type-hierarchy

+3

All Articles