Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (39 sloc) 1.68 KB


Open, single dispatch polymorphism.


Leiningen dependency information:

[name.stadig/polyfn "2.0.0"]

Maven dependency information:


Define a polyfn.

(require '[name.stadig.polyfn :refer [defpolyfn extend-polyfn]])
(defpolyfn foo [exp])

Define some implementations for specific types.

(extend-polyfn foo Long [exp] (inc exp))
(extend-polyfn 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.

(extend-polyfn 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!"

Open, single dispatch polymorphism. Enjoy!


Copyright (c) Paul Stadig. All rights reserved.
Portions Copyright (c) Paul Stadig and Rich Hickey. All rights reserved.


The use and distribution terms for this software are covered by the Eclipse
Public License 1.0 ( which can
be found in the file epl-v10.html at the root of this distribution. By using
this software in any fashion, you are agreeing to be bound by the terms of
this license.  You must not remove this notice, or any other, from this