The define-foreign-type and define-foreign-converter macros allow the definition of parameterized types. For example, assume you want to create a foreign type that matches the Lisp type unsigned-byte when supplied with an argument of one of 8, 16, or 32. The following code achieves this:
(fli:define-foreign-type foreign-unsigned-byte (&optional (bitsize '*)) (case bitsize (8 '(:unsigned :byte)) (16 '(:unsigned :short)) (32 '(:unsigned :int)) (otherwise (error "Illegal foreign type (~s ~s)" 'foreign-unsigned-byte bitsize))))
This defines the new foreign type foreign-unsigned-byte
that can be used anywhere within the FLI as one of:
(foreign-unsigned-byte 8)
(foreign-unsigned-byte 16)
(foreign-unsigned-byte 32)
Specifying anything else returns an error.
Foreign Language Interface User Guide and Reference Manual - 18 Feb 2025 15:36:31