► |  | The Racket Foreign Interface

1 | Overview
2 | Loading Foreign Libraries
3 | C Types
4 | Pointer Functions
5 | Derived Utilities
6 | Miscellaneous Support
7 | Unexported Primitive Functions
| Bibliography
| Index

▼ | 3 | C Types

3.1 | Type Constructors
3.2 | Numeric Types
3.3 | Other Atomic Types
3.4 | String Types
3.5 | Pointer Types
3.6 | Function Types
3.7 | C Struct Types
3.8 | C Array Types
3.9 | C Union Types
3.10 | Enumerations and Masks

On this page:

make-union-type
_ union
union?
union-ref
union-set!
union-ptr

top ← prev up next →

#### 3.9`` C Union Types

> procedure (make-union-type type ...+) → ctype?
> type : ctype?

The primitive type constructor for creating new C union types. Like C struct types, union types are new primitive types with no conversion functions associated. Unions are always treated like structs.

Example:

> > (make-union-type (_list-struct _int _int)
> (_list-struct _double _double))
> #<ctype:cstruct>

> procedure (_union type ...+) → ctype?
> type : ctype?

Creates a union type whose Racket representation is a union that works with union-ref and union-set!. The union is not copied; the Racket representation is backed by the underlying C representation.

Example:

> > (_union (_list-struct _int _int)
> (_list-struct _double _double))
> #<ctype>

> procedure (union? v) → boolean?
> v : any/c

Returns #t if v is a Racket representation of a C value via _union, #f otherwise.

Examples:

> > (define a-union-type
> (_union (_list-struct _int _int)
> (_list-struct _double _double)))
> > (define a-union-val
> (cast (list 3.14 2.71)
> (_list-struct _double _double)
> a-union-type))
> > (union? a-union-val)
> #t
> > (union? 3)
> #f

> procedure (union-ref u i) → any/c
> u : union?
> i : exact-nonnegative-integer?

Extracts a variant from a union. The variants are indexed starting at 0.

Examples:

> ; see examples for union? for definitions
> > (union-ref a-union-val 1)
> '(3.14 2.71)

> procedure (union-set! u i v) → void?
> u : union?
> i : exact-nonnegative-integer?
> v : any/c

Sets a variant in a union.

Examples:

> ; see examples for union? for definitions
> > (union-set! a-union-val 0 (list 4 5))
> > a-union-val
> #<union>
> > (union-ref a-union-val 0)
> '(4 5)

> procedure (union-ptr u) → cpointer?
> u : array?

Extracts the pointer for a union’s storage.

Example:

> > (union-ptr a-union-val)
> #<cpointer>

top ← prev up next →