Skip to content

Commit 0f06cfa

Browse files
authored
Fix issues with condp macro (#1138)
Fixes #1137
1 parent da1eaee commit 0f06cfa

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
### Fixed
1212
* Fix a bug where tags in data readers were resolved as Vars within syntax quotes, rather than using standard data readers rules (#1129)
1313
* Fix a bug where `keyword` and `symbol` functions did not treat string arguments as potentially namespaced (#1131)
14+
* Fix a bug where `condp` would throw an exception if a result expression was `nil` (#1137)
1415

1516
## [v0.3.2]
1617
### Added

src/basilisp/core.lpy

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3565,7 +3565,11 @@
35653565
- ``test-expr :>> result-fn`` where :>> is a keyword literal
35663566

35673567
For the ternary expression clause, the unary ``result-fn`` will be called with the
3568-
result of the predicate."
3568+
result of the predicate.
3569+
3570+
A single final expression can be included at the end with no test expression which
3571+
will be returned if no other clause matches the predicate. If no default is provided
3572+
and no clause matches the predicate, a ``ValueError`` will be thrown."
35693573
[pred expr & clauses]
35703574
(when (seq clauses)
35713575
(let [test-expr (first clauses)
@@ -3574,16 +3578,16 @@
35743578
(let [result (first remaining)
35753579
remaining (rest remaining)]
35763580
(cond
3577-
(= result :>>) `(let [res# ~(list pred test-expr expr)]
3578-
(if res#
3579-
(~(first remaining) res#)
3580-
(condp ~pred ~expr ~@(rest remaining))))
3581-
result `(if ~(list pred test-expr expr)
3582-
~result
3583-
(condp ~pred ~expr ~@remaining))
3584-
:else (throw
3585-
(ex-info "expected result expression"
3586-
{:test test-expr}))))
3581+
(not (seq remaining)) `(throw
3582+
(python/ValueError
3583+
(str "Expected result expression for condp " {:test ~test-expr})))
3584+
(= result :>>) `(let [res# ~(list pred test-expr expr)]
3585+
(if res#
3586+
(~(first remaining) res#)
3587+
(condp ~pred ~expr ~@(rest remaining))))
3588+
:else `(if ~(list pred test-expr expr)
3589+
~result
3590+
(condp ~pred ~expr ~@remaining))))
35873591
test-expr))))
35883592

35893593
(defmacro declare

tests/basilisp/test_core_macros.lpy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,16 @@
347347
(is (= :a (condp = "a"
348348
"b" :b
349349
"c" :c
350-
:a))))
350+
:a)))
351+
(is (nil? (condp = "a"
352+
"b" :b
353+
"a" nil
354+
"c" :c
355+
:a)))
356+
(is (thrown? python/ValueError
357+
(condp = "a"
358+
"b" :b
359+
"c" :c))))
351360

352361
(testing "condp result function"
353362
(is (= :a (condp some [1 8 10 12]

0 commit comments

Comments
 (0)