Merge pull request #717 from algernon/f/defn-better-errors
Better error messages for fn/defn w/o arglists
This commit is contained in:
commit
b35fd7d5da
@ -1893,6 +1893,9 @@ class HyASTCompiler(object):
|
||||
called_as = expression.pop(0)
|
||||
|
||||
arglist = expression.pop(0)
|
||||
if not isinstance(arglist, HyList):
|
||||
raise HyTypeError(expression,
|
||||
"First argument to (fn) must be a list")
|
||||
ret, args, defaults, stararg, kwargs = self._parse_lambda_list(arglist)
|
||||
|
||||
if PY34:
|
||||
|
@ -44,6 +44,8 @@
|
||||
"define a function `name` with signature `lambda-list` and body `body`"
|
||||
(if (not (= (type name) HySymbol))
|
||||
(macro-error name "defn/defun takes a name as first argument"))
|
||||
(if (not (isinstance lambda-list HyList))
|
||||
(macro-error name "defn/defun takes a parameter list as second argument"))
|
||||
`(setv ~name (fn ~lambda-list ~@body)))
|
||||
|
||||
|
||||
|
@ -52,6 +52,15 @@
|
||||
(try (eval '(defn lambda [] (print "hello")))
|
||||
(catch [e [TypeError]] (assert (in "Can't assign to a builtin" (str e))))))
|
||||
|
||||
(defn test-fn-corner-cases []
|
||||
"NATIVE: tests that fn/defn handles corner cases gracefully"
|
||||
(try (eval '(fn "foo"))
|
||||
(catch [e [Exception]] (assert (in "to (fn) must be a list"
|
||||
(str e)))))
|
||||
(try (eval '(defn foo "foo"))
|
||||
(catch [e [Exception]]
|
||||
(assert (in "takes a parameter list as second" (str e))))))
|
||||
|
||||
(defn test-for-loop []
|
||||
"NATIVE: test for loops"
|
||||
(setv count 0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user