Foreign Types

Alien types have a description language based on nested list structure. For example the C type

struct foo {
    int a;
    struct foo *b[100];
};
has the corresponding SBCL FFI type
(struct foo
  (a int)
  (b (array (* (struct foo)) 100)))

Defining Foreign Types

Types may be either named or anonymous. With structure and union types, the name is part of the type specifier, allowing recursively defined types such as:

(struct foo (a (* (struct foo))))
An anonymous structure or union type is specified by using the name nil. The with-alien macro defines a local scope which "captures" any named type definitions. Other types are not inherently named, but can be given named abbreviations using the define-alien-type macro.

Foreign Types and Lisp Types

The foreign types form a subsystem of the SBCL type system. An alien type specifier provides a way to use any foreign type as a Lisp type specifier. For example,

(typep foo '(alien (* int)))
can be used to determine whether foo is a pointer to a foreign int. alien type specifiers can be used in the same ways as ordinary Lisp type specifiers (like string.) Alien type declarations are subject to the same precise type checking as any other declaration.

Note that the type identifiers used in the foreign type system overlap with native Lisp type specifiers in some cases. For example, the type specifier (alien single-float) is identical to single-float, since foreign floats are automatically converted to Lisp floats. When type-of is called on an alien value that is not automatically converted to a Lisp value, then it will return an alien type specifier.

Foreign Type Specifiers

Note: All foreign type names are exported from the sb-alien package. Some foreign type names are also symbols in the common-lisp package, in which case they are reexported from the sb-alien package, so that e.g. it is legal to refer to sb-alien:single-float.

These are the basic foreign type specifiers: