--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
Wouldn't an annotation be a better fit?
Gili
How are you going to handle arrays? Marking them final doesn't guarantee that they'll be immutable.
Wouldn't an annotation be a better fit?
--
Craig Berry
Software Engineer
Google (Santa Monica CA)
If you can subclass it, it's not immutable anyway--Immutable classes ought to be final (see, e.g. Effective Java, Item 13).
Expect resistance adding reserved words. It seems clear that all fields in an immutable class would have to be immutable (and labelled as such). You'd have the compiler complain if there is a field of type "List" and not "ImmutableList". Arrays would be forbidden
I don't think it's reasonable to expect that deep immutability could
be enforced in this way, given how Java works. Surface immutability
would be very useful in its own write. Just declare the class
immutable, and all members are required to be final, and all
subclasses must also be immutable.
On Tuesday, July 12, 2011 6:42:29 AM UTC+8, Raymond Rishty wrote:If you can subclass it, it's not immutable anyway--Immutable classes ought to be final (see, e.g. Effective Java, Item 13).
I disagree with Effective Java in that respect. I believe it is quite acceptable to be able to subclass an immutable class.
Requiring all fields to be final would prevent java.lang.String from
qualifying as an immutable class.
Even though it is "effectively immutable", the hashCode is lazily
initialized, making it formally mutable.
Defining immutability is like nailing Jello to the wall.
Your mutability detector might be alright in a production environment where its use is enforced,
however, if you produce a class library for public use, then you have no way of enforcing
compliance in other environments.
If we have immutable classes, then compliance is automatically enforced, no matter the
environment that your class library is used in.
The cost is adding the expense of the hashCode function to String construction. As most Strings (particularly the big ones) aren't compared or used as keys and the hashCode function is O(n) where n is the length of the String, this could be a significant (as in noticeable) unnecessary overhead. For more details of this kind of thing see: http://jeremymanson.blogspot.com/2008/12/benign-data-races-in-java.html
We really really _don't_ want to get rid of accessor methods, even on final immutable classes. There are still benefits having the binary link to an accessor method rather than the field, particularly if you are creating public API, as you can change the implementation and remain binary compatible. The JIT is quite capable of removing any method calls and inlining as it sees fit anyway so the only thing you save is typing out the method definition. If you really don't like doing that I suggest you look at a language like Scala that removes the need entirely.
On 12 July 2011 00:10, BradW <br...@willcott.com> wrote:Your mutability detector might be alright in a production environment where its use is enforced,
however, if you produce a class library for public use, then you have no way of enforcing
compliance in other environments.
Just because it uses static analysis doesn't mean it cannot be used at runtime - you could inspect parameters to your library, and their runtime class, the way you would inspect a reference for nullness. That would enforce compliance in any environment. Performing the analysis is obviously not zero cost, but it only needs to be done once per class per execution, which is manageable in a lot of enviroments.
If we have immutable classes, then compliance is automatically enforced, no matter the
environment that your class library is used in.
How about environments using a previous version of Java? Would the keyword essentially cut you off from being binary compatible?
Here's an example of using an annotation to force a condition on a subclass - and that enforcement happens at compile time. Will this approach not work for @Immutable ?
http://www.javaspecialists.eu/archive/Issue167.html
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group:...
Here's an example of using an annotation to force a condition on a subclass - and that enforcement happens at compile time. Will this approach not work for @Immutable ?
Does the benefit (for such a small number of classes) outweigh the
cost?
I like features like @NotNull because I can see myself using it on
the majority of fields and method parameters (let's be honest, the
language's default should really be @NotNull instead of null). As a
side-topic, what ever happened to JSR 303? Is it possible to specify an
annotation on a class-level and have all fields default to @NotNull
instead of having to annotate each one individually?
Gili
> --
> guava-...@googlegroups.com
> Project site: http://guava-libraries.googlecode.com
> This group: http://groups.google.com/group/guava-discuss
>
> This list is for general discussion.
> To report an issue: http://code.google.com/p/guava-libraries/issues/entry
I like the idea in general, I just think it has deeper ramifications than your proposal suggests, and consensus is one of them, unfortunately.
You may also be interested in this: http://bugs.sun.com/view_bug.do?bug_id=4617197
--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")
--