Issue APPLY-EXAMPLE-WRONG
Issue: APPLY-EXAMPLE-WRONG

Forum: Editorial

References: apply

Category: CLARIFICATION/CHANGE

Edit history: 2011-01-02, Version 3 by Dave Fox

Status: For CLiki consideration

Problem Description:

This example for APPLY is wrong, because it assumes particular behavior of TYPE-OF:
(defun foo (size &rest keys &key double &allow-other-keys)
   (let ((v (apply #'make-array size :allow-other-keys t keys)))
     (if double (concatenate (type-of v) v v) v)))
(foo 4 :initial-contents '(a b c d) :double t)
   =>  #(A B C D A B C D)
The call to FOO will signal an error if TYPE-OF returns a type containing the length (4). This causes a type conflict in CONCATENATE, because the new sequence length should be 8. (If TYPE-OF returns a type without the length, then FOO will behave as shown above.)

Proposal (APPLY-EXAMPLE-FIX):

To get the intended effect, it could do something like one of these:

(concatenate `(vector ,(array-element-type v) *) v v)
(concatenate `(vector ,(array-element-type v) ,(* 2 size)) v v)

Test case:

As above.

Rationale:

Fix broken example.

Current practice:

The example signals error in LispWorks, at least.

Cost to Implementors:

None.

Cost to Users:

None.

Cost of Non-Adoption:

Users may inadvertently write unportable code based on this example.

Benefits:

Aesthetics:

Discussion: