From 432d5603109f6bee7882215931a05e5580537e63 Mon Sep 17 00:00:00 2001 From: David Schaefer Date: Fri, 4 Aug 2017 17:08:41 +0200 Subject: [PATCH 1/2] No TypeError from multi-arity defn returning None --- NEWS | 1 + hy/contrib/multi.hy | 8 ++++---- tests/native_tests/contrib/multi.hy | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index f89b885..6580965 100644 --- a/NEWS +++ b/NEWS @@ -25,6 +25,7 @@ Changes from 0.13.0 * Fixed a bug where REPL history wasn't saved if you quit the REPL with `(quit)` or `(exit)` * `exec` now works under Python 2 + * No TypeError from multi-arity defn returning values evaluating to None Changes from 0.12.1 diff --git a/hy/contrib/multi.hy b/hy/contrib/multi.hy index 8c89486..c0cee3f 100644 --- a/hy/contrib/multi.hy +++ b/hy/contrib/multi.hy @@ -26,13 +26,13 @@ (.issubset (frozenset (.keys kwargs)) com))) __call__ (fn [self &rest args &kwargs kwargs] - (setv output None) + (setv func None) (for [[i f] (.items (get self._fns self.f.__module__ self.f.__name__))] (when (.fn? self i args kwargs) - (setv output (f #* args #** kwargs)) + (setv func f) (break))) - (if output - output + (if func + (func #* args #** kwargs) (raise (TypeError "No matching functions with this signature"))))]) (defn multi-decorator [dispatch-fn] diff --git a/tests/native_tests/contrib/multi.hy b/tests/native_tests/contrib/multi.hy index 9b0294c..04fe41e 100644 --- a/tests/native_tests/contrib/multi.hy +++ b/tests/native_tests/contrib/multi.hy @@ -26,6 +26,22 @@ (assert (= (fun "a" "b") "a b")) (assert (= (fun "a" "b" "c") "a b c"))) +(defn test-different-signatures-defn [] + "NATIVE: Test defn with different signatures" + (defn fun + ([] "") + ([a] "a") + ([a b] "a b")) + + (assert (= (fun) "")) + (assert (= (fun "a") "a")) + (assert (= (fun "a" "b") "a b")) + (try + (do + (fun "a" "b" "c") + (assert False)) + (except [e Exception] + (assert (isinstance e TypeError))))) (defn test-basic-dispatch [] "NATIVE: Test basic dispatch" From 9056506e307e770b10692f1ec845eb8a63357b84 Mon Sep 17 00:00:00 2001 From: David Schaefer Date: Fri, 4 Aug 2017 17:11:37 +0200 Subject: [PATCH 2/2] Updated AUTHORS --- AUTHORS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 2c1f274..dcc9b0c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -78,4 +78,5 @@ * John Patterson * Kai Lüke * Neil Lindquist \ No newline at end of file +* Hikaru Ikuta +* David Schaefer