Skip to content

Commit bb7c470

Browse files
authored
Permit default expresssions in case without clauses (#808)
Hi, could you please review patch to allow `case` to work with exclusive default expressions. Fixes #807. This is also to maintain Clojure compatibility. Thanks Co-authored-by: ikappaki <[email protected]>
1 parent 5f8e245 commit bb7c470

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
* Generate Python classes for `deftype*` and `reify*` forms using modern `@attr.define`, `@attr.frozen`, and `@attr.field` APIs (#799)
2323
* Generate Protocol functions with nicer names based on the protocol function and dispatch type (#803)
2424
* Loosen the dependency specification for Immutables and Pyrsistent to allow for a wider version range (#805)
25+
* Allow `case` forms with only a default expression (#807)
2526

2627
### Fixed
2728
* Fix issue with `(count nil)` throwing an exception (#759)

src/basilisp/core.lpy

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,16 +3402,14 @@
34023402
(defmacro case
34033403
"Switch on ``expr`` to return a matching clause from the set of input clauses.
34043404

3405-
The input expression may be any valid Basilisp expression.
3405+
The input expression may be any valid Basilisp expression. A single default expression
3406+
can follow the clauses, and its value will be returned if no clause matches.
34063407

34073408
The clauses are pairs of a matching value and a return value. The matching values are
34083409
not evaluated and must be compile-time constants. Symbols will not be resolved. Lists
34093410
may be passed to match multiple compile time values to a single return value. The
34103411
dispatch is done in constant time."
34113412
[expr & clauses]
3412-
(when (< (count clauses) 2)
3413-
(throw (ex-info "case expression must have at least one clause"
3414-
{:clauses clauses})))
34153413
(let [default (if (odd? (count clauses))
34163414
(last clauses)
34173415
:basilisp.core.case/no-default)

tests/basilisp/test_core_macros.lpy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@
326326
(is (= "also no" (case 'c :a "no" (b c d) "also no" "duh")))
327327
(is (= "also no" (case 'd :a "no" (b c d) "also no" "duh")))
328328
(is (= "duh" (case 'e :a "no" (b c d) "also no" "duh")))
329+
(is (= 0 (case 1 0)))
329330
(let [out* (atom [])]
330331
(is (= 2 (case :2
331332
:1 (do (swap! out* conj 1) 1)

0 commit comments

Comments
 (0)