Should I use a function or a macro to validate arguments in Clojure?

后端 未结 3 2048
慢半拍i
慢半拍i 2020-12-29 10:40

I have a group of numeric functions in Clojure that I want to validate the arguments for. There are numerous types of arguments expected by the functions, such as positive i

3条回答
  •  星月不相逢
    2020-12-29 10:56

    A case where you need a macro would be if you wanted to modify the language to automatically add the tests to any function defined within a block, like this:

    (with-function-validators [test1 test2 test4]  
        (defn fun1 [arg1 arg2] 
            (do-stuff))
        (defn fun2 [arg1 arg2] 
            (do-stuff))
        (defn fun3 [arg1 arg2] 
            (do-stuff)))  
    

提交回复
热议问题