Skip to content

Commit 406a624

Browse files
committed
Let case and condp throw RuntimeExceptions when no clause matches.
1 parent 0c1ae91 commit 406a624

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

pixie/stdlib.pxi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,7 +2166,7 @@ Expands to calls to `extend-type`."
21662166
If the result is truthy returns the second value of the clause.
21672167

21682168
If the number of arguments is odd and no clause matches, the last argument is returned.
2169-
If the number of arguments is even and no clause matches, nil is returned."
2169+
If the number of arguments is even and no clause matches, throws an exception."
21702170
[pred-form expr & clauses]
21712171
(let [x (gensym 'expr), pred (gensym 'pred)]
21722172
`(let [~x ~expr, ~pred ~pred-form]
@@ -2175,18 +2175,18 @@ Expands to calls to `extend-type`."
21752175
(if (> (count clause) 1)
21762176
`((~pred ~a ~x) ~b)
21772177
`(:else ~a)))
2178-
(partition 2 clauses))))))
2178+
(partition 2 clauses))
2179+
:else (throw "No matching clause!")))))
21792180

21802181
(defmacro case
21812182
"Takes an expression and a number of two-form clauses.
21822183
Checks for each clause if the first part is equal to the expression.
21832184
If yes, returns the value of the second part.
21842185

2185-
The first part of each clause can also be a set. If that is the case, the clause
2186-
matches when the result of the expression is in the set.
2186+
The first part of each clause can also be a set. If that is the case, the clause matches when the result of the expression is in the set.
21872187

21882188
If the number of arguments is odd and no clause matches, the last argument is returned.
2189-
If the number of arguments is even and no clause matches, nil is returned."
2189+
If the number of arguments is even and no clause matches, throws an exception."
21902190
[expr & args]
21912191
`(condp #(if (set? %1) (%1 %2) (= %1 %2))
21922192
~expr ~@args))

tests/pixie/tests/test-stdlib.pxi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,9 @@
421421
{1 2, 2 2, 3 2, 4 1}))
422422

423423
(t/deftest test-condp
424-
(t/assert= (condp :dont-call-me :dont-use-me) nil)
424+
(t/assert-throws? RuntimeException
425+
"No matching clause!"
426+
(condp :dont-call-me :dont-use-me))
425427
(let [f (fn [x]
426428
(condp = x
427429
1 :one
@@ -432,7 +434,9 @@
432434
(t/assert= (f 9) :whatever)))
433435

434436
(t/deftest test-case
435-
(t/assert= (case :unused) nil)
437+
(t/assert-throws? RuntimeException
438+
"No matching clause!"
439+
(case :no-matter-what))
436440
(let [f (fn [x]
437441
(case x
438442
1 :one

0 commit comments

Comments
 (0)