(ancestors ClassName) does not include tag ancestors of ClassName's superclasses

2 views
Skip to first unread message

Chas Emerick

unread,
Mar 9, 2009, 8:47:09 PM3/9/09
to Clojure
The subject mostly says it all. Example:

;;;;;;;;;;;
user=> (ancestors String)
#{java.io.Serializable java.lang.Object java.lang.CharSequence
java.lang.Comparable}
user=> (ancestors Object)
nil
user=> (derive String ::foo)
nil
user=> (derive Object ::bar)
nil
user=> (isa? String ::foo)
true
user=> (isa? String ::bar)
true
user=> (ancestors String)
#{java.io.Serializable java.lang.Object java.lang.CharSequence :user/
foo java.lang.Comparable}
user=> (ancestors Object)
#{:user/bar}
;;;;;;;;;;;

I'd expect that ::bar would be included in the set returned by
(ancestors String). A similar interaction involving only keywords:

;;;;;;;;;;
user=> (derive ::p ::gp)
nil
user=> (derive ::c ::p)
nil
user=> (ancestors ::c)
#{:user/p :user/gp}
;;;;;;;;;;

I'll open an issue with a patch if I'm correct in thinking that the
current behaviour of ancestors isn't correct in not including the tag
ancestors of superclasses.

- Chas

Chas Emerick

unread,
Mar 10, 2009, 8:10:22 AM3/10/09
to clo...@googlegroups.com
FWIW, a version of ancestors that exhibits proper (IMO) behaviour:

(defn ancestors
([tag] (ancestors global-hierarchy tag))
([h tag] (not-empty
(let [ta (get (:ancestors h) tag)]
(if (class? tag)
(let [superclasses (set (supers tag))]
(apply clojure.set/union
ta
superclasses
(map #(get (:ancestors h) %) superclasses)))
ta)))))

- Chas

Reply all
Reply to author
Forward
0 new messages