简体   繁体   English

如何覆盖有序集的.toString方法

[英]How to override .toString method for ordered-set

I'm using ordered-set for some stuff in Clojure 1.3.0. 我在Clojure 1.3.0中使用了有序集。 My project.clj includes [ordered "1.1.0"] . 我的project.clj包括[ordered "1.1.0"]

When I call (str (ordered-set 1 2 3)) I get "ordered.set.OrderedSet@6" but when I call a normal clojure hash-set (str (hash-set 1 2 3)) I get the correct "#{1 2 3}" as a result. 当我调用(str (ordered-set 1 2 3))我得到"ordered.set.OrderedSet@6"但是当我调用普通的clojure哈希集(str (hash-set 1 2 3))我得到了正确的"#{1 2 3}"

The str method calls .toString somewhere, so how do I override the .toString method for ordered-set so I can get a proper string out of it? str方法在某处调用.toString ,那么如何覆盖ordered-set.toString方法,以便我可以从中获取正确的字符串?

thanks 谢谢

Well, there are two answers to this question. 嗯,这个问题有两个答案。 One is, the lazy maintainer of ordered (me) should get his act together and implement toString . 一个是, ordered (我)的懒惰维护者应该一起行动并实现toString The other is, you probably shouldn't be using str on anything where you care about readability - pr-str is a much more reliable data-presentation function. 另一种是,你可能不应该在任何关心可读性的地方使用str - pr-str是一种更可靠的数据表示功能。 For example, (str {:a 'a}) and (str {:a "a"}) both result in "{:aa}" . 例如, (str {:a 'a})(str {:a "a"})都会产生"{:aa}" With pr-str the type information remains. 使用pr-str ,类型信息仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM