Overwrite equals in defrecord

477 views
Skip to first unread message

Marc Dzaebel

unread,
Oct 22, 2013, 5:22:10 PM10/22/13
to clo...@googlegroups.com
(defrecord R [x y]) automatically defines a reasonable equals method using x & y. However, is it possible to overwrite the method as it should use X only? My tries resulted in "Duplicate method name&signature in class ...". Do I have to use extend-type?

Marc Dzaebel

unread,
Oct 22, 2013, 5:44:50 PM10/22/13
to clo...@googlegroups.com
http://cmayes.wikispaces.com/PracticalClojure13: ...
defrecord does not support Java class inheritance, so it cannot override methods of Java classes, even abstract classes. However, it does permit you to override methods of java.lang.Object such as hashCode, equals, and toString. Simply include java.lang.Object in the defrecord as if it were an interface. Clojure will generate good value-based implementations of the hashCode and equals methods, so it is rarely necessary to implement them yourself.

(defrecord R [x y] java.lang.Object (equals [_ r](= x(.x r))))
--> Duplicate method name&signature in class file

Jim - FooBar();

unread,
Oct 22, 2013, 7:24:33 PM10/22/13
to clo...@googlegroups.com
use deftype which is more low-level and I think doesn't define equals or put y in meta data that don't participate in equality...:)

Jim
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Marc Dzaebel

unread,
Oct 25, 2013, 10:41:38 AM10/25/13
to clo...@googlegroups.com
>> use deftype which is more low-level and I think doesn't define equals or put y in meta data that don't participate in equality...:)

Hi Jim,

this works! However, if you need the multiple features of records, you would need to have the generated code of defrecord in order to reimplement records with deftype. Is there something available?

Thanks, Marc

Andrii V. Mishkovskyi

unread,
Oct 25, 2013, 10:56:32 AM10/25/13
to clo...@googlegroups.com

On Fri, Oct 25, 2013 at 4:41 PM, Marc Dzaebel <mdza...@web.de> wrote:
However, if you need the multiple features of records, you would need to have the generated code of defrecord in order to reimplement records with deftype. Is there something available?

Hey Marc,

I've recently been using this little library potemkin which has a few useful deftype-like macros that would save a lot of typing (albeit I had a different use case from yours), maybe deftype+ will work for you: https://github.com/ztellman/potemkin#def-abstract-type-and-deftype

Cheers.


--
Wbr, Andrii V. Mishkovskyi.

He's got a heart of a little child, and he keeps it in a jar on his desk.

Reply all
Reply to author
Forward
0 new messages