WuHoUnited
WuHoUnited

Reputation: 8429

Clojure: Implementing Seqable for an existing java class

I am trying to make a core java class implement an interface. I am trying something along the lines of:

(extend-protocol clojure.lang.Seqable
  java.lang.Integer
  (seq [this] (seq (str this))))

but this does not seem to work because Seqable is just an interface and not a protocol. Is it possible to make (seq 123) work? how was seq implemented for java.lang.Strings?

proxy also does not seem capable of doing this.

I know I must be missing somethnig really obvious here.

Upvotes: 4

Views: 602

Answers (2)

NielsK
NielsK

Reputation: 6956

If the java class implements Iterable, and is wrapped in seq, you could use it as a sequence, with certain restrictions.

Upvotes: 0

amalloy
amalloy

Reputation: 92117

Not possible. clojure.lang.RT/seqFrom has special cases for a number of java builtin types, like Collection and String, and you can't add your own for classes that don't implement Seqable directly.

Upvotes: 2

Related Questions