Skip to content

Commit 99fab72

Browse files
committed
Disallow duplicate method defs in proxy macro
1 parent 18ba940 commit 99fab72

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

src/basilisp/core.lpy

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6964,7 +6964,6 @@
69646964

69656965
;; TODO: kwargs on supertypes
69666966
;; TODO: check interface/superclass method membership
6967-
;; TODO: check if duplicate methods
69686967
(defmacro proxy
69696968
"Create a new proxy class instance.
69706969

@@ -7000,21 +6999,30 @@
70006999
(let [formatted-single (fn [method-name [arg-vec & body]]
70017000
[(munge method-name)
70027001
(apply list 'fn method-name (vec (concat ['this] arg-vec)) body)])
7003-
formatted-multi (fn [method-name & arities]
7004-
[(munge method-name)
7005-
(apply list
7006-
'fn
7007-
method-name
7008-
(map (fn [[arg-vec & body]]
7009-
(apply list (vec (concat ['this] arg-vec)) body))
7010-
arities))])
7011-
methods (mapcat (fn [[method-name & body]]
7012-
(if (vector? (first body))
7013-
(formatted-single method-name body)
7014-
(apply formatted-multi method-name body)))
7015-
fs)]
7002+
formatted-multi (fn [method-name & arities]
7003+
[(munge method-name)
7004+
(apply list
7005+
'fn
7006+
method-name
7007+
(map (fn [[arg-vec & body]]
7008+
(apply list (vec (concat ['this] arg-vec)) body))
7009+
arities))])
7010+
methods (map (fn [[method-name & body]]
7011+
(if (vector? (first body))
7012+
(formatted-single method-name body)
7013+
(apply formatted-multi method-name body)))
7014+
fs)
7015+
method-map (reduce* (fn [m [method-name method]]
7016+
(if-let [existing-method (get m method-name)]
7017+
(throw
7018+
(ex-info "Cannot define proxy class with duplicate method"
7019+
{:method-name method-name
7020+
:impls [existing-method method]}))
7021+
(assoc m method-name method)))
7022+
{}
7023+
methods)]
70167024
#_(println methods)
7017-
`((get-proxy-class ~@class-and-interfaces) ~(apply hash-map methods) ~@args)))
7025+
`((get-proxy-class ~@class-and-interfaces) ~method-map ~@args)))
70187026

70197027
(defmacro proxy-super
70207028
"Macro which expands to a call to the method named ``meth`` on the superclass

tests/basilisp/test_proxies.lpy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
(is (thrown? basilisp.lang.exception/ExceptionInfo
2121
(construct-proxy python/object)))))
2222

23+
(definterface Describable
24+
(describe-me []))
25+
2326
(definterface ToString
2427
(to-string [])
2528
(to-string [arg1])
@@ -33,6 +36,11 @@
3336
(to-string [this arg1 & rest] (str "rest" arg1 rest)))
3437

3538
(deftest proxy-test
39+
(testing "disallows duplicate method overrides"
40+
(proxy [Describable] []
41+
(describe-me [] "I'm a proxy")
42+
(describe-me [] "Proxy")))
43+
3644
(testing "multi-arity interface methods"
3745
(let [p (proxy [ConcreteToString] [1]
3846
(to-string

0 commit comments

Comments
 (0)