Creating array with elements of a custom type

Discussion of Common Lisp

Creating array with elements of a custom type

Postby TPJ » Tue Dec 30, 2008 6:28 am

Had a hard day coding in CL and looking for basic knowledge on this language.

Let's assume I'll define a structure:

Code: Select all
(defstruct my-Str
  (slot-1 0 :type fixnum)
  (slot-2 0.0d0 :type double-float))


It's a new type. How can I make an array of elements of that type? I've tried (make-array (list 5) :element-type 'my-Str), but all I got was an array of type T.
TPJ
 
Posts: 11
Joined: Tue Nov 25, 2008 6:37 am
Location: Gliwice, Poland

Re: Creating array with elements of a custom type

Postby Ramarren » Tue Dec 30, 2008 7:10 am

The purpose of array types is to allow implementation to use specialized memory layout for the array. The specification allows the type to be upgraded to most specific such type (see http://www.lispworks.com/documentation/HyperSpec/Body/f_upgr_1.htm). In the case of custom types no specialized layout is possible in general (custom type redefinition is allowed at any time), so the array is just an array of pointers, that is, of type T.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: Creating array with elements of a custom type

Postby dlweinreb » Tue Jan 06, 2009 4:24 am

Just don't specify an element type. Then the array elements can hold any Lisp object.

(It's better not to use the word "pointer" when talking about Lisp, I've found.)
dlweinreb
 
Posts: 41
Joined: Tue Jul 01, 2008 5:11 am
Location: Lexington, MA


Return to Common Lisp

Who is online

Users browsing this forum: Bing [Bot] and 2 guests