Skip to content

Type macros ​

One of the most powerful features of Lisp is its macro system, which makes it possible to add custom syntax to the language. Similarly, et has type-macros, which enable custom syntax for writing types.
Defining a type-macro is very similar to defining a standard Lisp macro. Its definition is a function whose arguments and return value are unparsed types. Type-macros operate on unparsed types, much like how Lisp macros operate on unevaluated expressions.

emacs-lisp
(et-defsyntax tuple (&rest args)
  (if (null args) 'Nil
    `(Cons ,(car args) (tuple ,@(cdr args)))))

(et tuple 1 2 3)
;; -> (Cons 1 (Cons 2 (Cons 3 Nil)))
;; A read-only equivalent will also be auto-generated
(et &tuple 1 2 3)
;; -> (&Cons 1 (&Cons 2 (&Cons 3 Nil)))

To extend the analogy, if type-macros are like Lisp macros, then type-operations are like functions. Type-macros can define custom syntax, but type-operations are still necessary for the underlying type manipulations.

Both args and fn, which we have already seen, are type-macros. A more complete list can be found here.