repr is a top-level attribute that specifies the Common Lisp runtime representation of a define-type, define-struct, or define-exception.
Syntax
(repr :enum)
(repr :transparent)
(repr :lisp)
(repr :native ⟨lisp-type-specifier⟩)
Semantics
reprmust appear immediately beforedefine-type,define-struct, ordefine-exception(or separated from it only by aderiveattribute). At most onereprattribute is allowed per definition.:enum:- Valid on
define-typewhen every constructor is nullary (has zero fields). - Represents each constructor
Cof typeTat runtime as the symbolT/C, with Common Lisp type(cl:member T/C1 T/C2 ...). - Avoids heap allocation and allows
matchto compile tocl:casejump tables.
- Valid on
:transparent:- Valid on
define-typeordefine-structwhen the type has exactly one constructor with exactly one field(C τ). - Eliminates any runtime wrapper: the runtime representation of the type is identical to that of
τ, the constructorCcompiles to an inlined identity function, and struct field accessors compile toid.
- Valid on
:lisp:- Valid on
define-type. - Guarantees a distinct heap-allocated class or structure (
Tand constructor subtypesT/C) across both development and release modes so values can be specialized in CLOScl:defmethoddefinitions and tested withcl:typep.
- Valid on
:native ⟨lisp-type-specifier⟩:- Valid on
define-type(typically with zero constructors) anddefine-exception(with zero constructors). - Maps the Coalton type directly to
⟨lisp-type-specifier⟩in emitted Common Lisp type declarations andRuntimeReprinstances. - On
define-exception,⟨lisp-type-specifier⟩must be a compile-time subtype ofcl:serious-condition.
- Valid on
define-structonly accepts(repr :transparent).define-exceptiononly accepts(repr :native ⟨lisp-condition-type⟩).define-type-aliasanddefine-resumptiondo not acceptrepr.
Examples
(coalton-toplevel
;; Symbol-backed enumeration
(repr :enum)
(define-type Color
Red
Green
Blue)
;; Zero-overhead newtype wrapper
(repr :transparent)
(define-type Meters
(Meters F64))
;; Foreign Common Lisp type wrapper
(repr :native cl:cons)
(define-type (LispPair :car :cdr))
(declare make-pair (forall (:car :cdr) :car * :cdr -> (LispPair :car :cdr)))
(define (make-pair x y)
(lisp (-> (LispPair :car :cdr)) (x y)
(cl:cons x y))))
Other notes
- Without an explicit
reprattribute, Coalton compilesdefine-typeanddefine-structdefinitions as CLOS classes in development mode and frozencl:defstructtypes in release mode. - Parametric opaque native types (
(repr :native ...) (define-type (T :a))) have their type parameters inferred as invariant because Coalton cannot inspect their internal mutability. - See Lisp Interop for details on runtime representation guarantees.