I created a library for Clojure to do open, single dispatch
polymorphism. What does this mean?

* A polyfn dispatches on the type of its first argument.
* You can add an implementation for a new type to an existing polyfn.
* You can define a new polyfn on an existing type.

Polyfns are exactly as fast as protocol functions (I am using the same
caching and dispatch code), but they do not generate a Java interface,
and they are slightly simpler to define.

You can find polyfn here https://github.com/pjstadig/polyfn. You can
pull it using Leiningen like so:

 [name.stadig/polyfn "1.0.2"]

Or using Maven like so:

 <dependency>
   <groupId>name.stadig</groupId>
   <artifactId>polyfn</artifactId>
   <version>1.0.2</version>
 </dependency>

EXAMPLE:

Define some implementations for specific types.

 (require '[name.stadig.polyfn :refer [defpolyfn]])
 (defpolyfn foo Long [exp] (inc exp))
 (defpolyfn foo String [exp] "Hello, World!")

Use it.

 (foo 1) => 2
 (foo "string") => "Hello, World!"
 (foo 1.0) => #<IllegalArgumentException
java.lang.IllegalArgumentException: No implementation of polyfn:
#'user/foo found for class: java.lang.Double>

Oops, there is no implementation for Double, let’s define one for
java.lang.Number.

 (defpolyfn foo Number [exp] Number)
 (foo 1.0) => java.lang.Number

The rest of the implementations remain the same

 (foo 1) => 2
 (foo "string") => "Hello, World!"

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