Could proved immutable classes be added to Scala?

View: New views
1 Messages — Rating Filter:   Alert me  

Could proved immutable classes be added to Scala?

by Erkki Lindpere-2 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Since Scala makes a point of allowing both the immutable/functional and
mutable/imperative styles of programming, it could benefit from a means
of saying formally whether a class is immutable or not. Of course, it
could be mentioned in ScalaDoc comments, and made sure by the
implementers, but I think something *formal* and (somewhat) *proved*
would be better. And could probably lead to some optimization
possibilities or better tools in the future.

For example, an annotation @immutable or trait Immutable that could be
added to types, objects of which are intended to be immutable. The
compiler would check that these classes meet certain conditions that
prove them immutable, otherwise raise an error.
Coming from imperative programming, most recently Java (and having had
very little to do with FP), I may be displaying my ignorance, but as far
as I know, the following should be enough to prove a Scala class
immutable (I'm sure many people here know better if this is wrong):
1) it doesn't extend any mutable classes (only extends those that were
already proved immutable)
2) it doesn't declare any variable members
3) it doesn't declare mutable constructor arguments
4) it doesn't declare mutable value members, maybe not even lazy values?
5) is final or it's subclasses must also be immutable
6) function types are not considered immutable
(optional 7) doesn't use any mutable classes in any way at all -- this
might not be required, I think, it probably depends on what level of
immutability is wanted)

Some side effects/notes:
* Any class that extends an immutable class must be immutable
* like a case class, equals and hashCode should be generated based on
constructor arguments (but if manually written, these methods would not
be checked for correctness of course)
* java.lang.String must be specially marked immutable somehow, also
possibly other classes that I'm missing

Would this be very hard to implement? Or does it even make sense?

Erkki