pop
函数的文档说明如下:
user> (doc pop)
-------------------------
clojure.core/pop
([coll])
For a list or queue, returns a new list/queue without the first
item, for a vector, returns a new vector without the last item. If
the collection is empty, throws an exception.
然而,我无法重现应该抛出异常的行为。
例如,在此处,我将三个元素添加到队列中,然后pop
五次:根据文档,这不应该起作用。 但是,我没有遇到异常,而是得到了nil。
(peek (pop (pop (pop (pop (pop (conj (conj (conj clojure.lang.PersistentQueue/EMPTY 4) 5) 6)))))))
现在我很喜欢当尝试从空队列
pop
时,返回一个空队列而不是抛出异常的行为,但我想知道为什么这种行为与文档不同(至少从我阅读文档的理解来看)。基本上,我想知道是否应该在此处“保护”自己免受异常影响,或者我是否可以安全地假定
pop
一个空队列将始终返回一个空队列(这与文档相矛盾)。