Need help with syntax for implementing multiple arity protocol method

3,127 views
Skip to first unread message

James

unread,
Aug 4, 2010, 3:59:28 PM8/4/10
to Clojure
I am having trouble getting syntax right when defining a protocol
function/method with multiple arities. When the defrecord is
evaluated I get:

java.lang.ClassCastException: clojure.lang.PersistentList cannot be
cast to clojure.lang.Symbol
[Thrown class java.lang.RuntimeException]

I have tried different variations, but cannot seem to get it right.
Please point out what I am doing wrong.

The repl:
user> *clojure-version*
{:major 1, :minor 2, :incremental 0, :qualifier "RC1"}
user> (defprotocol protocol-two
(method-two [x] [x y]))
protocol-two
user> (defrecord record-two [r]
(method-two ([x] (str x))
([x y] (str x ":" y))))


Thanks for the help.

Randy Hudson

unread,
Aug 4, 2010, 8:57:06 PM8/4/10
to Clojure
You need to name the protocol before the method implementations.
That's why the "PersistentList cannot be cast to Symbol" message --
the compiler's expecting a protocol (or interface) symbol after [r].

And, as the examples on http://clojure.org/protocols show, you need to
define the different arities of method-two separately:

(defrecord record-two [r]
protocol-two
(method-two [x] (str x))
(method-two [x y] (str x ":" y)))

Also note that all the examples use capitalized names for protocols
and records. And I'm dubious about the hyphens in those names, too. I
would have gone with ProtocolTwo (or Protocol2) and Record2.
Reply all
Reply to author
Forward
0 new messages