Skip to content

Commit ee39f48

Browse files
authored
Coerce booleans using Basilisp's rules, not Python's rules (#929)
Fixes #928
1 parent fe3f027 commit ee39f48

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
* Fix a bug where the core functions `symbol` and `keyword` would not accept non-string data types (#911)
2121
* Fix a bug where the compiler would emit warnings on when a Var was redef'ed even if that Var was initially defined with `^:redef` metadata (#916)
2222
* Fix a bug where reader column offset numbering began at 1, rather than 0 (#905)
23+
* Fix a bug where `basilisp.core/boolean` was returning the boolean coercions like Python rather than like Basilisp (#928)
2324

2425
### Other
2526
* Add several sections to Concepts documentation module (#666)

src/basilisp/core.lpy

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@
478478
(recur (rest s))
479479
(first s)))
480480

481-
(defn ^:inline nil?
481+
(defn ^:inline nil?
482482
"Return ``true`` if ``x`` is ``nil``\\, otherwise ``false``\\."
483483
[x]
484484
(operator/is- x nil))
@@ -1578,9 +1578,14 @@
15781578
(python/int x))
15791579

15801580
(defn ^:inline boolean
1581-
"Coerce ``x`` to a boolean."
1581+
"Coerce ``x`` to a boolean.
1582+
1583+
Only ``false`` and ``nil`` are logical false and will return ``false``\\. All
1584+
other values will return ``true``\\."
15821585
[x]
1583-
(python/bool x))
1586+
(if (or (nil? x) (false? x))
1587+
false
1588+
true))
15841589

15851590
(defn byte
15861591
"Coerce ``x`` to a byte."
@@ -2088,6 +2093,9 @@
20882093
;;;;;;;;;;;;;;;;;;;;;;;;;;
20892094

20902095
(defn bounded-count
2096+
"Return the count (as by :lpy:fn:`count`) of any ``coll`` for which ``counted?``
2097+
returns ``true``, otherwise return the lesser of ``n`` and the length of
2098+
``coll``\\."
20912099
[n coll]
20922100
(if (counted? coll)
20932101
(count coll)

tests/basilisp/test_core_fns.lpy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,36 @@
126126
(is (= v1 v2))
127127
(is (= {:tag vector} (meta v2))))))))
128128

129+
(deftest boolean-test
130+
(testing "logical false values"
131+
(are [v] (false? (boolean v))
132+
false
133+
nil))
134+
135+
(testing "logical true values"
136+
(are [v] (true? (boolean v))
137+
true
138+
1
139+
0
140+
-1
141+
1.0
142+
0.0
143+
-1.0
144+
""
145+
"false"
146+
[]
147+
[false]
148+
#{}
149+
#{false}
150+
{}
151+
{false false}
152+
'()
153+
'(false false)
154+
:kw
155+
:ns/kw
156+
'sym
157+
'ns/sym)))
158+
129159
(deftest compare-test
130160
(testing "nil"
131161
(are [res x y] (= res (compare x y))

0 commit comments

Comments
 (0)