On Aug 12, 2009, at 3:24 PM, Chas Emerick wrote:

>> I'd hate to see somebody do it, but it's currently valid to define
>> variables with colons in them, so there could be code out in the wild
>> with those definition forms already.  Other than that, I like the
>> looks of it.  I think the macro would be easy as well, so long as
>> you're comfortable munging variable names :)
>
> Well, that's one benefit/motivation to just have another def form,
> rather than trying to make defn support everyone's preferred  
> variation/
> style.
>
> I knocked out a quickie implementation that appears to work with the
> couple of use cases I've thrown at it so far (pasted at the end of
> this msg).

I've been fiddling with this, and produced a slight variant of my  
original macro that uses Type:arg-name symbols instead of arg- 
name:Type symbols. Example:

(defh a
   [b:String [c:Double :as list:java.util.List] {d:java.util.Random :d}]
   (.toCharArray b)
   (.size list)
   (.floatValue c)
   (.nextInt d))

I've actually been using it at the REPL here and there, and I've found  
it pretty pleasant -- it's a very pretty way to hint args compared to  
#^, and arg:Type ordering makes for easy readability (e.g. you can  
very easily scan an arg form and see what the names of the args are,  
as opposed to "actively" parsing them due to #^ hints or scanning for  
the (relatively unobtrusive) colon in the Type:arg style).

Code below.

Cheers,

- Chas

;;;;;;;;;;

(defn- decorate-hinted-symbol
   [sym]
   (let [[arg type] (.split (name sym) ":")]
     (if type
       (with-meta (symbol arg) {:tag (symbol type)})
       sym)))

(defn- decorate-hinted-args
   [args]
   (cond
     (vector? args) (vec (map decorate-hinted-args args))
     (map? args) (into {} (map decorate-hinted-args args))
     (symbol? args) (decorate-hinted-symbol args)
     (keyword? args) args
     :else (throw (Exception. (str args)))))

(defmacro defh
   [name args & body]
   `(defn ~name
      ~(decorate-hinted-args args)
      ~...@body))

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to