Peter Norvig famously claimed that many OOP design patterns are trivial or redundant in Lisp. Slide #10 here claims that first-class types replace many of them.
In what sense are types first-class in Common Lisp? For reference, Structure and Interpretation of Computer Programs gives the following as the conditions that an element of a programming language must satisfy to be considered first-class:
They may be named by variables.
They may be passed as arguments to procedures.
They may be returned as the results of procedures.
They may be included in data structures
a demonstration that types in Common Lisp satisfy the above conditions would be a wonderful answer.
Types are not first-class in Common Lisp: the language exposes no object which represents a type and thus trivially fails all the requirements you give.
Classes are first-class and classes correspond to a subset of types.
Type specifiers are also first-class: a type specifier like integer
or (integer 0 (10))
is obviously first-class because symbols and lists are first-class. But type specifiers are not types, any more than the symbol cons
or the list (lambda (x) x)
is a function.
So Norvig is wrong about this.
Types aren't a distinct data type in Common Lisp, they're represented using type specifiers. These are symbols (e.g. FIXNUM
or RANDOM-STATE
), lists (e.g. (INTEGER 3 5)
or (AND LIST (NOT NULL))
, and class objects (e.g. the value of (FIND-CLASS 'STANDARD-CLASS)
), which are all first-class objects.
You can pass these around and then use them when calling something like TYPEP
:
(let ((some-class 'FIXNUM))
(print (typep 3 some-class)))
You can also create new type specifier names using the DEFTYPE
macro. However, there's no standard functional interface to this. In this respect types are somewhat second-class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With