A few days ago, Chouser and I had a discussion on IRC about the viability of named arguments (a la Smalltalk) for Clojure. In clojure- contrib, there is the macro "defnk" which provides this sort of capability, but it's performance characteristics are worse than than normal function call. I wanted to try my hand at a better performing macro and here is what I came up with:
(defmacro named-call [fn & named-args] (let [named-args-map (apply hash-map named-args) meta-data (eval `^(var ~fn)) arg-names (:arg-names meta-data) arg-list (for [arg-key arg-names] (get named-args-map arg- key))] `(~fn ~...@arg-list))) It is used on the call site instead of the definition like so: (defn subtract [from take] (- from take)) (call subtract :from 10 :take 2) The macro assumes some meta data on the function variable which describes the argument list keyed by :arg-names like so: (def #^{:arg-names [:from :take]} subtract subtract) The macro isn't as sophisticated as I'd like yet. It doesn't deal with multiple function signatures and I'd like a way to define default values. I'd also like it to return useful error messages if the caller's signature is wrong. Thanks to Chouser, Rich and others from IRC for helping me with this stuff. Even if the macro isn't all that valuable, I learned a lot about Clojure in the process. If anyone has any suggestions, I'd love to hear them. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---