Skip to content

Commit 518c95d

Browse files
authored
Fix import errors in multi-methods (#223)
* Fix the litany of errors that occurred in diagnosing multi-methods * Rectify test failures
1 parent a5c2598 commit 518c95d

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

src/basilisp/compiler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ def _import_ast(ctx: CompilerContext, form: llist.List) -> ASTStream:
965965

966966
def _interop_call_ast(ctx: CompilerContext, form: llist.List) -> ASTStream:
967967
"""Generate a Python AST node for Python interop method calls."""
968-
assert form[0] == _INTEROP_CALL
968+
assert form.first == _INTEROP_CALL
969969
assert form[1] is not None
970970
assert isinstance(form[2], sym.Symbol)
971971
assert form[2] is not None
@@ -987,7 +987,7 @@ def _interop_call_ast(ctx: CompilerContext, form: llist.List) -> ASTStream:
987987

988988
def _interop_prop_ast(ctx: CompilerContext, form: llist.List) -> ASTStream:
989989
"""Generate a Python AST node for Python interop property access."""
990-
assert form[0] == _INTEROP_PROP
990+
assert form.first == _INTEROP_PROP
991991
assert form[1] is not None
992992
assert isinstance(form[2], sym.Symbol)
993993
assert form[2].ns is None
@@ -1020,7 +1020,7 @@ def let_32(a_33, b_34, c_35):
10201020
10211021
let_32(a_36, b_37, c_38) #=> {:length 1}
10221022
"""
1023-
assert form[0] == _LET
1023+
assert form.first == _LET
10241024
assert isinstance(form[1], vec.Vector)
10251025
assert len(form) >= 3
10261026

src/basilisp/core/__init__.lpy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,14 +1385,14 @@
13851385
body)
13861386
dispatch-fn (first body)
13871387
opts (apply hash-map (rest body))]
1388-
`(def ~name (basilisp.lang.multifn/MultiFunction ~name
1388+
`(def ~name (basilisp.lang.multifn/MultiFunction ~(quote name)
13891389
~dispatch-fn
1390-
(or (:default opts) :default)))))
1390+
~(or (:default opts) :default)))))
13911391

13921392
(defmacro defmethod
13931393
"Add a new method to the multi-function which responds to dispatch-val."
13941394
[multifn dispatch-val & fn-tail]
1395-
`(.add-method multi-fn dispatch-val (fn ~@fn-tail)))
1395+
`(. ~multifn ~'add-method ~dispatch-val (fn ~@fn-tail)))
13961396

13971397
(defn methods
13981398
"Return a map of dispatch values to methods for the given multi function."
@@ -1414,6 +1414,6 @@
14141414

14151415
(defn remove-all-methods
14161416
"Remove all method for this multi-function. Return the multi function."
1417-
[multifn dispatch-val]
1417+
[multifn]
14181418
(.remove-all-methods multifn)
14191419
multifn)

src/basilisp/lang/runtime.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ class Namespace:
239239
'basilisp.lang.keyword',
240240
'basilisp.lang.list',
241241
'basilisp.lang.map',
242+
'basilisp.lang.multifn',
242243
'basilisp.lang.runtime',
243244
'basilisp.lang.seq',
244245
'basilisp.lang.set',

tests/basilisp/test_multifn.lpy

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
(ns basilisp.multifn-test
2+
(:require
3+
[basilisp.test :refer [deftest is testing]]))
4+
5+
(defmulti test-fn
6+
(fn [v]
7+
(cond
8+
(= v "i") :a
9+
(= v "ii") :b
10+
:else :default)))
11+
12+
(defmethod test-fn :a
13+
[_]
14+
"1")
15+
16+
(defmethod test-fn :b
17+
[_]
18+
"2")
19+
20+
(defmethod test-fn :default
21+
[_]
22+
"BLAH")
23+
24+
(deftest test-multi-functions
25+
(testing "multi method usage"
26+
(is (= "1" (test-fn "i")))
27+
(is (= "2" (test-fn "ii")))
28+
(is (= "BLAH" (test-fn "iii")))
29+
(is (= "BLAH" (test-fn "other"))))
30+
31+
(testing "multi-method generic assertions"
32+
(is (map? (methods test-fn)))
33+
(is (seq (methods test-fn))))
34+
35+
(testing "multi method usage after removal"
36+
(remove-method test-fn :b)
37+
38+
(is (= "1" (test-fn "i")))
39+
(is (= "BLAH" (test-fn "ii")))
40+
(is (= "BLAH" (test-fn "iii")))
41+
(is (= "BLAH" (test-fn "other"))))
42+
43+
(testing "removing all methods"
44+
(remove-all-methods test-fn)
45+
46+
(is (not (seq (methods test-fn))))))

0 commit comments

Comments
 (0)