如何在Common Lisp中忽略返回值?处理ADJUST-ARRAY的CMUCL警告
Great question! Let’s break this down clearly—first, why CMUCL is giving you that warning, then how to properly ignore the return value if you’re sure that’s what you want.
Why the Warning?
Even though many Common Lisp implementations (including CMUCL) will modify the array in place most of the time, the Common Lisp standard does not require adjust-array to return the same array object. There are edge cases where it might hand back a new array instead:
- If the original array wasn’t marked as adjustable (though you’d likely get an error before reaching this point)
- If adjusting the array’s size, element type, or other properties can’t be done in-place (e.g., switching to an element type incompatible with the original array’s storage)
CMUCL’s warning is a heads-up: ignoring the return value could lead to silent bugs if you’re relying on your original variable pointing to the adjusted array, but a new object was returned instead.
How to Ignore the Return Value Properly
If you’ve weighed the risks and still want to suppress the warning (e.g., you’re certain your use case guarantees in-place modification), here are the cleanest, idiomatic ways to do it:
1. Bind to a throwaway variable and declare it ignored
This is the most straightforward approach. Use a variable name like _ (a common convention for values you intend to ignore) and explicitly tell the compiler you don’t care about it:
(let ((my-array (make-array 3 :adjustable t))) (let ((_ (adjust-array my-array 6))) (declare (ignore _)) ; Continue working with my-array here ))
2. Wrap the call in a progn with a dummy return value
If you prefer not to use a variable, wrap the adjust-array call in a progn that returns something else (like nil). This signals to the compiler that you’re intentionally discarding the return value:
(progn (adjust-array my-array 6) nil)
3. Use ignore-errors (use with caution!)
Note: This method will also suppress any errors thrown by adjust-array, so only use it if you specifically want to ignore both errors and the return value:
(ignore-errors (adjust-array my-array 6))
A Quick Best Practice Reminder
Even if you’re ignoring the return value now, it’s worth considering whether you should actually use it instead. For example, if you ever switch implementations or adjust your array in a way that triggers a new object creation, using the return value will save you from hard-to-debug issues later. Here’s the safe, future-proof approach:
(setf my-array (adjust-array my-array 6))
This ensures my-array always points to the adjusted array—whether it’s the original object or a new one.
内容的提问来源于stack exchange,提问作者Robert Dodier

