Skip to main content

array-element-type

array-element-type Function

Syntax:

array-element-type array → typespec

Arguments and Values:

array—an array.

typespec—a type specifier .

Description:

Returns a type specifier which represents the actual array element type of the array, which is the set of objects that such an array can hold. (Because of array upgrading, this type specifier can in some cases denote a supertype of the expressed array element type of the array.)

Examples:

(array-element-type (make-array 4)) → T 
(array-element-type (make-array 12 :element-type(unsigned-byte 8)))
→ implementation-dependent
(array-element-type (make-array 12 :element-type(unsigned-byte 5)))
→ implementation-dependent
(array-element-type (make-array 5 :element-type(mod 5)))
could be (mod 5), (mod 8), fixnum, t, or any other type of which (mod 5) is a *subtype*.

Affected By:

The implementation.

Exceptional Situations:

Should signal an error of type type-error if its argument is not an array.

See Also:

array, make-array, subtypep, upgraded-array-element-type

Expanded Reference: array-element-type

Basic Usage

array-element-type returns a type specifier representing the actual element type of the array, which may be broader than what you requested due to array upgrading.

;; General arrays have element type T
(array-element-type (make-array 5))
=> T

(array-element-type (vector 1 2 3))
=> T

Specialized Arrays

For arrays created with :element-type, the actual element type depends on the implementation's upgrading rules.

;; Strings have character element types
(array-element-type "hello")
=> CHARACTER

;; Bit vectors have element type BIT
(array-element-type #*10110)
=> BIT

;; Requested type may be upgraded
(array-element-type (make-array 4 :element-type 'bit))
=> BIT

Implementation-Dependent Upgrading

Implementations may upgrade requested types. For instance, (unsigned-byte 5) might be upgraded to (unsigned-byte 8) or even fixnum.

;; The actual element type may be broader than requested
(let ((a (make-array 4 :element-type '(unsigned-byte 8) :initial-element 0)))
(subtypep '(unsigned-byte 8) (array-element-type a)))
=> T
=> T

Checking Array Specialization

You can use array-element-type to determine whether an array is specialized for a particular type.

(let ((general (make-array 3 :initial-element 0))
(bits (make-array 3 :element-type 'bit :initial-element 0))
(chars (make-array 3 :element-type 'character :initial-element #\a)))
(list (array-element-type general)
(array-element-type bits)
(array-element-type chars)))
=> (T BIT CHARACTER)