Records implementing IFn

733 views
Skip to first unread message

Mark Fredrickson

unread,
Feb 8, 2011, 12:08:08 AM2/8/11
to Clojure
Is the following behavior correct or a bug:

user> (defrecord Example [data] clojure.lang.IFn (invoke [this] this)
(invoke [this n] (repeat n this)))
user.Example
user> (def e (Example. "I am e"))
#'user/e
user> e
#:user.Example{:data "I am e"}
user> (e 2)
(#:user.Example{:data "I am e"} #:user.Example{:data "I am e"})
user> (e)
#:user.Example{:data "I am e"}
user> (def some-es (e 5))
; Throws java.lang.AbstractMethodError

I can't tell if I am doing something wrong with respect to the IFn
definition, or if I am encountering a bug.

I have not tried building up a deftype by hand. That may well solve my
problem and would be a perfectly acceptable solution. I started with a
record to hold immutable data and implement the meta-data protocol,
but I could implement these on a type (perhaps dropping another
protocol/interface if it is causing the problem above).

Thanks in advance,
-Mark

Alex Osborne

unread,
Feb 8, 2011, 12:55:31 AM2/8/11
to clo...@googlegroups.com
Mark Fredrickson <mark.m.fr...@gmail.com> writes:

> Is the following behavior correct or a bug:
>
> user> (defrecord Example [data] clojure.lang.IFn (invoke [this] this)
> (invoke [this n] (repeat n this)))
> user.Example
> user> (def e (Example. "I am e"))
> #'user/e

> user> (e 2)
> (#:user.Example{:data "I am e"} #:user.Example{:data "I am e"})

> user> (def some-es (e 5))
> ; Throws java.lang.AbstractMethodError
>
> I can't tell if I am doing something wrong with respect to the IFn
> definition, or if I am encountering a bug.

I think you need to implement applyTo for a properly working IFn
implementation:

(defrecord Example [data] clojure.lang.IFn
(invoke [this] this)
(invoke [this n] (repeat n this))

(applyTo [this args] (clojure.lang.AFn/applyToHelper this args)))

Mark Fredrickson

unread,
Feb 8, 2011, 9:05:11 AM2/8/11
to Clojure
Thank you. That seems to do the trick.

-Mark


On Feb 7, 11:55 pm, Alex Osborne <a...@meshy.org> wrote:
Reply all
Reply to author
Forward
0 new messages