Fun with Macros: With-EOF-Handled

Posted on June 23rd, 2024.

It's been a while (again), but it's time to take a look at another little Common Lisp macro that I find myself using fairly often: with-eof-handled.

  1. Usage
  2. Implementation
  3. Result

Usage

Common Lisp has several functions for reading from character streams, e.g. read-char and read-line. The arguments for these functions follow a common pattern where they take (&optional input-stream eof-error-p eof-value) (and one more argument that's not relevant here). By default, calling one of these functions will signal an error if an end of file occurs. You can change this by passing true as the eof-error-p argument (which, despite its -p suffix should be a (generalized) boolean, not a predicate), in which case the eof-value will be returned instead of signaling an error.

Implementation

Result