Skip to content

Commit cc606e8

Browse files
authored
callable var (#768)
Hi, could you please review compatibility patch with Clojure to make vars callable. It addresses #767. I am not sure this if the `class Var` is the right place to make vars callable or the analyzer, which should expand them to a callable var value last node. Nevertheless, I will kindly request your help with the type hinting, currently it will fail the linter with the following error ``` src/basilisp/lang/runtime.py:279:15: E1102: self.value is not callable (not-callable) ``` but not sure how to fix it, I've tried the class Var `value` property method to return a maybe Callable but it didn't work. Thanks Co-authored-by: ikappaki <[email protected]>
1 parent 1a744a0 commit cc606e8

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
### Changed
1212
* Optimize calls to Python's `operator` module into their corresponding native operators (#754)
13+
* Allow vars to be callable to adhere to Clojure conventions (#767)
1314

1415
### Fixed
1516
* Fix issue with `(count nil)` throwing an exception (#759)

src/basilisp/lang/runtime.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ def __init__(
275275
def __repr__(self):
276276
return f"#'{self.ns.name}/{self.name}"
277277

278+
def __call__(self, *args, **kwargs):
279+
return self.value(*args, *kwargs) # pylint: disable=not-callable
280+
278281
@property
279282
def ns(self) -> "Namespace":
280283
return self._ns

tests/basilisp/core_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,11 @@ def test_is_var():
951951
assert False is core.var__Q__(core.list_)
952952

953953

954+
def test_is_var_callable():
955+
varlist = runtime.Var.find(sym.symbol("list", ns="basilisp.core"))
956+
assert llist.l(2, 3) == varlist(2, 3)
957+
958+
954959
class TestExceptionData:
955960
def test_ex_cause_for_non_exception(self):
956961
assert None is core.ex_cause("a string")

tests/basilisp/test_core_fns.lpy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,14 @@
14111411
(testing "ns-not-exists"
14121412
(is (thrown? python/AttributeError (intern 'ns-non-existant 'xyz)))))
14131413

1414+
;;;;;;;;;;
1415+
;; Vars ;;
1416+
;;;;;;;;;;
1417+
1418+
(deftest var-test
1419+
(testing "calling vars"
1420+
(is (= '(1 2) (#'basilisp.core/list 1 2)))))
1421+
14141422
;;;;;;;;;;;;;;;;;
14151423
;; Hierarchies ;;
14161424
;;;;;;;;;;;;;;;;;

0 commit comments

Comments
 (0)