diff --git a/CHANGELOG.md b/CHANGELOG.md index c66a2ca7..41298ad3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed * Fix a bug where tags in data readers were resolved as Vars within syntax quotes, rather than using standard data readers rules (#1129) * Fix a bug where `keyword` and `symbol` functions did not treat string arguments as potentially namespaced (#1131) + * Fix a bug where `condp` would throw an exception if a result expression was `nil` (#1137) ## [v0.3.2] ### Added diff --git a/src/basilisp/core.lpy b/src/basilisp/core.lpy index 63aa4868..484c9fd8 100644 --- a/src/basilisp/core.lpy +++ b/src/basilisp/core.lpy @@ -3565,7 +3565,11 @@ - ``test-expr :>> result-fn`` where :>> is a keyword literal For the ternary expression clause, the unary ``result-fn`` will be called with the - result of the predicate." + result of the predicate. + + A single final expression can be included at the end with no test expression which + will be returned if no other clause matches the predicate. If no default is provided + and no clause matches the predicate, a ``ValueError`` will be thrown." [pred expr & clauses] (when (seq clauses) (let [test-expr (first clauses) @@ -3574,16 +3578,16 @@ (let [result (first remaining) remaining (rest remaining)] (cond - (= result :>>) `(let [res# ~(list pred test-expr expr)] - (if res# - (~(first remaining) res#) - (condp ~pred ~expr ~@(rest remaining)))) - result `(if ~(list pred test-expr expr) - ~result - (condp ~pred ~expr ~@remaining)) - :else (throw - (ex-info "expected result expression" - {:test test-expr})))) + (not (seq remaining)) `(throw + (python/ValueError + (str "Expected result expression for condp " {:test ~test-expr}))) + (= result :>>) `(let [res# ~(list pred test-expr expr)] + (if res# + (~(first remaining) res#) + (condp ~pred ~expr ~@(rest remaining)))) + :else `(if ~(list pred test-expr expr) + ~result + (condp ~pred ~expr ~@remaining)))) test-expr)))) (defmacro declare diff --git a/tests/basilisp/test_core_macros.lpy b/tests/basilisp/test_core_macros.lpy index c820a8c8..f57cbb7b 100644 --- a/tests/basilisp/test_core_macros.lpy +++ b/tests/basilisp/test_core_macros.lpy @@ -347,7 +347,16 @@ (is (= :a (condp = "a" "b" :b "c" :c - :a)))) + :a))) + (is (nil? (condp = "a" + "b" :b + "a" nil + "c" :c + :a))) + (is (thrown? python/ValueError + (condp = "a" + "b" :b + "c" :c)))) (testing "condp result function" (is (= :a (condp some [1 8 10 12]