DEFTYPE — 파생 타입 지정자 정의하기
DEFTYPE — 파생 타입 지정자 정의하기 (매크로)
새 derived type specifier를 정의하는 매크로예요. 타입 지정자를 받아 다른 타입 지정자로 확장하는 함수를 통해, 여러분만의 의미 있는 타입 이름을 만들고 typep·declare·typecase 등 어디서든 그 이름으로 타입을 표현할 수 있어요.
시그니처 (Syntax)
deftype name lambda-list [[declaration* | documentation]] form* => name
name—symbol.lambda-list— deftype lambda list.declaration— declare expression. 평가되지 않아요.documentation—string. 평가되지 않아요.form—form.
본문 (Description)
deftype는 name이라는 derived type specifier를 정의해요.
새 type specifier의 의미는, 그 type specifier를 다른 type specifier로 확장하는 함수로 표현돼요. 그 확장 결과가 또 다른 derived type specifier를 참조하면 다시 확장돼요.
새로 정의된 type specifier는 (name arg1 arg2 ...) 형태의 list로 참조될 수 있어요. 인자 수는 lambda-list에 맞아야 해요. 새 type specifier가 인자를 안 받거나, 인자가 모두 optional이면 atomic type specifier로 쓸 수 있어요.
type specifier의 argument expression(arg1 ... argn)은 evaluate되지 않아요. 대신 이 literal object들이 대응하는 parameter가 bound될 object가 돼요.
deftype form의 본문(lambda-list는 아님)은 name이라는 block 안에 암묵적으로 감싸여 있고, implicit progn으로 평가되어 새 type specifier를 돌려줘요.
본문의 lexical environment는 deftype form이 평가될 당시 유효했던 환경에 lambda-list의 variable들을 더한 것이에요.
확장으로 돌려준 type specifier의 재귀 확장은 반드시 끝나야 해요. 여기에는 확장 안에 중첩된 type specifier의 확장도 포함돼요.
type specifier를 완전히 확장한 결과에, member·eql type specifier가 참조하는 object 안을 제외하고 순환 구조가 포함되면 결과는 정의되지 않아요.
documentation은 name에 type 종류의 documentation string으로 붙어요.
deftype form이 top level form으로 나타나면, compiler는 이후 type 선언에서 name이 인식되도록 보장해야 해요. programmer는 이후 type 선언에서 name이 참조된다면 deftype form의 본문이 컴파일 시점에 evaluate될 수 있게 보장해야 해요. type specifier의 확장이 컴파일 시점에 완전히 정의되지 않으면(예: 알 수 없는 type specifier로 확장되거나, 컴파일 타임 환경에 정의되지 않은 이름 붙은 function의 satisfies로 확장되는 경우), implementation은 선언에서 이 type에 대한 참조를 무시하거나 경고를 신호할 수 있어요.
예제 (Examples)
(defun equidimensional (a)
(or (< (array-rank a) 2)
(apply #'= (array-dimensions a)))) => EQUIDIMENSIONAL
(deftype square-matrix (&optional type size)
`(and (array ,type (,size ,size))
(satisfies equidimensional))) => SQUARE-MATRIX
더 알아보기 (See Also)
declare, defmacro, documentation, Section 4.2.3 (Type Specifiers), Section 3.4.11 (Syntactic Interaction of Documentation Strings and Declarations)
Notes
없음.