Re: Road to Clojure Survey



On Feb 23, 9:53 am, Alessio Stalla <alessiosta...@xxxxxxxxx> wrote:

Sorry for stepping into this discussion from nowhere,

That's how we all got here. :-)


but I'd like to point out that, although the general contract
for Java collections requires providing an initializing
constructor taking another Collection as a parameter, AFAIK
such a constructor is generally implemented using the addAll()
method (see for example
http://kickjava.com/src/java/util/ArrayList.java.htm
), so Pascal's statement (that some widely used operations
on Java collections require mutation) is not wrong in principle.

I don't think Pascal was complaining about the internal implementation
details of the constructors. I think he was saying that the
interfaces required him to create an empty object and then add all the
items directly. Please see the text I quoted, or Pascal's original
messages, if you disagree with me.


OTOH I suspect that
Clojure's collections will provide that very same constructor
implemented using non-mutating operations.

Someone's got to build it up piece by piece somewhere, but that's an
implementation detail, not an interface one.


Collections.sort(...) won't work on Clojure's immutable collections.

It should throw an UnsupportedOperationException - exactly as it is
documented to do in this case.


Also, while it's true that mutating operations are "optional", in
practice common use of collections in Java involves mutating them.
Immutable collections are not idiomatic (they're not idiomatic in
Common Lisp either, for that matter, even if in general Common Lisp
favours a more "functional" use of sequences/collections).

Without some pretty ugly hackery, algorithms can't mutate a collection
instance that doesn't want to be mutated. Even though common Java
idiom is to implement mutable objects, the JDK Collection interfaces
and algorithms explicitly allow immutable ones, and the JVM will
enforce that immutability to a degree beyond most other programming
environments.


So, as always truth lays in the middle :)

I'm not sure if you're just trying to keep the peace, but I don't
think it lies that close to center in this case. Pascal's a smart
guy, but I think he misremembered a few things. Maybe he's recalling
JDK 1.1 - that was before the Collection interfaces were defined. The
Java implementers possibly introduced the Collection APIs to deal with
exactly the sorts of problems that Pascal was unhappy about.


That said, having Clojure's collections follow the Java
Collections interfaces is certainly a Good Thing, even
if one admits they're not "seamlessly integrated" with
the collections framework.

"Seamlessly" is a pretty subjective word. However, given the
constraints, I'm not sure how you'd go about making it any more
seamless than it is.


Cheers,
-Scott
.



Relevant Pages

  • Re: 2 newbie questions
    ... > the volume of a fish tank and a third that ties everything together. ... You create a .java file. ... Then you modify this by asking it to create a constructor method and calling ... When you have a series of variables in some array, and you have an array for ...
    (comp.lang.java.help)
  • Re: New (as in days) to Java - question about "super()" method
    ... > Hi, I'm Don, and I'm a serious greenie when it comes to Java. ... learn Java because Object Oriented Programming is quite different from ... > guts out of the method involved (a constructor, I believe, is the ... inheritance: if I have a class and I want to add some extra ...
    (comp.lang.java.programmer)
  • Re: Implicit overloads, non static
    ... you've disputed whether it's a matter of implementation rather ... that's clearer - but I wasn't disputing that. ... > I find a lot of things hard in Java. ... I'm not sure I'd call that a copy constructor myself, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: operator new with "value type"
    ... both C++ and Java that don't hold in C# ... ... construct to differentiate "create an auto variable" vs. "create and ... that such a term exists in C#) - it's a case of calling a constructor. ... an accent from another language - if you try to write C# as if it's C++ ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Road to Clojure Survey
    ... way of creating an empty collection and then adding elements to it. ... was introduced in Java 1.2 which was released near the end of 1998. ... Clojure's collection classes which implement the Collection interfaces ... would work with any of these operations that don't require mutation. ...
    (comp.lang.lisp)