;
; Recently someone said that it would be nice if Newlisp macro
; or function can check whether someone tried to supply more
; arguments than it is necessary. For example, if you accidentally
; call (sin x y), then sin complains that it got too many arguments.
;
; Such functions can be easily defined. But, it is also interesting
; that addition of the test can be automatized, because Newlisp
; function is equal to its definition.
;
; In this code, I defined macro forbid-args that inserts the code
; that throws an error in the body of supplied function.
(set 'forbid-args
(lambda-macro(f)
(push (letex ((message (append "Too many arguments in "
(string f))))
'(when (> (length $args) 0)
(throw-error message)))
(eval f)
1)))
(set 'my-macro (lambda-macro(x y)
(println "I got " x " and " y "." )))
(println my-macro)
;
; (lambda-macro (x y) (println "I got " x " and " y "."))
;
(my-macro 6 7 8)
; I got 6 and 7.
(forbid-args my-macro)
(println my-macro)
; (lambda-macro (x y)
; (when (> (length $args) 0)
; (throw-error "Too many arguments in my-macro"))
; (println "I got " x " and " y "."))
(my-macro 6 7 8)
; ERR: user error : Too many arguments in my-macro
Test for Unnecessary Arguments.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment