Skip to content

Commit d0b46c9

Browse files
authored
keyword function return default in error cases (#995)
Fixes #997 Quick fix. I noticed that using a keyword as a function with `nil` as the collection always returns `nil` rather than the default. This demonstrates the bug: ```clojure (:foo nil :bar) ;; Expected => :bar ;;Actual => nil ```
1 parent 296aec6 commit d0b46c9

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
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
* Fixed inconsistent behavior with `basilisp.core/with` when the `body` contains more than one form (#981)
2323
* Fixed an issue with `basilisp.core/time` failing when called outside of the core ns (#991)
2424
* Fixed an issue with `basilisp.core/promise` where a thread waiting for a value from another thread might not wake up immediately upon delivery (#983).
25+
* Fixed using keyword as a function not returning the default value in some cases (#997)
2526

2627
### Removed
2728
* Removed `python-dateutil` and `readerwriterlock` as dependencies, switching to standard library components instead (#976)

src/basilisp/lang/keyword.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __call__(self, m: Union[IAssociative, IPersistentSet], default=None):
7272
try:
7373
return m.val_at(self, default)
7474
except (AttributeError, TypeError):
75-
return None
75+
return default
7676

7777
def __reduce__(self):
7878
return keyword_from_hash, (self._hash, self._name, self._ns)

tests/basilisp/keyword_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def test_keyword_as_function():
6464
assert None is kw(lset.s(1))
6565
assert "hi" is kw(lset.s(1), default="hi")
6666

67+
assert 1 is kw(None, 1)
68+
assert None is kw(None, None)
69+
6770
assert None is kw(lvector.v(1))
6871

6972

0 commit comments

Comments
 (0)