I don't think there is currently a way to do this that is simultaneously:
1) Generic (dispatches on the type of all arguments)
2) Extensible (can be extended to new types at runtime)
3) Fast (about as fast as JVM dispatch allows)

Protocols give you 2) and 3). Multimethods give you 1) and 2). Custom 
solutions with instance checks and type hints can give you 1) and 3).

A good solution in my view would allow you to do things like this:

;; define a function with type-hinted overrides
(defn generic-function
  ([^Char c ^String cs] (....))
  ([^Object o ^List os] (.....)))

;; extend the function dynamically (protocol style)
(extend-fn generic-function ([^double d ^doubles ds] (....)))

This would imply quite a bit of code generation to compile the right 
efficient dispatch logic. Extension would also imply recompilation. So not 
totally trivial to implement, but I still think it would be a great 
addition to Clojure (either in core or as a library). It would be best if 
it was in core, because then the compiler could take advantage of type 
information to generate direct calls rather than doing dynamic dispatch in 
the relatively common situation that the type of arguments is already known.

On Saturday, 3 August 2013 12:22:27 UTC+1, Răzvan Rotaru wrote:
>
> Hi,
>
> I'm looking for fast lisp style generic functions in clojure. In other 
> words: multimethods that dispatch on the java type.
> A search on the web revealed little, since protocols and multimethods 
> always show up. I have also seen some old discussion in this group on the 
> topic, but I couldn't figure out whether there is a usable implementation 
> out there. So I'm posting my question on this group.
>
> 1/ Is there generic function implementation out there that I can use? 
> 2/ Are there plans to include them in future versions of clojure? 
> 3/ And lastly, could this be implemented as a sequence of single dispatch 
> (using the available single dispatch, which is fast)?
>
> Thanks,
> Răzvan
>

-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to