EOFを取得したい - lisp-cookbook-ja/common-lisp GitHub Wiki

テストケースの作成等でEOFの挙動をテストしたい場合、空のストリームが必要になりますが、空のストリームは、make-concatenated-streamで0個のストリームを連結したり、make-string-input-streamに空文字列を与えることで作ることができます。

(read (make-concatenated-stream))
;!! end of file on #<CONCATENATED-STREAM :STREAMS NIL {10293A1523}>
;!!    [Condition of type END-OF-FILE]


(read (make-string-input-stream ""))
;!! end of file on #<SB-IMPL::STRING-INPUT-STREAM {1027C414E3}>
;!!   [Condition of type END-OF-FILE]


(let ((stream (make-concatenated-stream)))
  (list (handler-case (read stream)
          (end-of-file () "eof"))
        (handler-case (read-line stream)
          (end-of-file () "eof"))
        (handler-case (read-char stream)
          (end-of-file () "eof"))))
;=>  ("eof" "eof" "eof")