On Apr 13, 2010, at 9:37 AM, Dan wrote:
I'm a Java developer; I work on a project that has adopted Clojure's data structures as a convenient implementation of immutable sets, maps, etc. In attempting to add type parameters to the Clojure interfaces, I noticed a conflict in the definition of IPersistentVector. I assume the problem exists when programming in Clojure, too.
You should note that by doing so you are superimposing homogeneity on collections that are otherwise heterogeneous.
IPersistentVector implements both Associative and IPersistentStack. These, in turn, both implement Seqable. But the element type in the two cases is different: if I have a vector of Strings, IPersistentStack says that seq() should enumerate the strings; Associative says that seq() should enumerate Map.Entries.
As you note below, they don't say any such thing.
The APersistentVector implementation resolves this conflict by going with the first: seq() gives you a list of strings. Put another way, the problem is that if I have an Associative which I know maps integers to strings, and I invoke seq(), I don't know what kind of values I'm going to get.
Objects
When I say an interface "says" something, there's not actually much documentation, so that's based on what I think is the natural interpretation of the interface. The page at <http://clojure.org/ data_structures> does say this about IPersistentMap: "seq returns a sequence of map entries, which are key/value pairs." There is no such assertion made about Associatives, but since IPersistentMap implements Seqable through Associative, it would be odd to specify the contract for Associative differently. It looks to me like the best way to handle this would be to eliminate the connection between Associative and IPersistentCollection, leaving that connection to the subtypes. But I don't know what kind of impact that would have on existing code (if it breaks something, does that code behave well when given an IPersistentVector?)
One way to look at the Seqable in high-level interfaces is just as specifying a sequence of whatever the value type is for the ultimate concrete implementation. Java supports return type covariance, so you could put a more specific signature on seq for IPersistentMap.
In any case, Associative shouldn't constrain the seq or value type of the collection. All it specifies is that, given keys you can get items.
Rich -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
