Adding an @Immutable annotation to Java
Simon Roberts
simon at dancingcloudservices.com
Mon Nov 29 18:42:30 UTC 2021
I will say that as Java provides more and more features modeled on more
functional languages, I'm finding more and more people struggling with the
inherently mutable nature of Java, and that I strongly believe that
something that helps bridge the gap is likely to be crucial to the
language's continued improvement and growth. Clearly it can't turn into
Haskell, nor should it, but without saying what, may I add my voice to
those looking for this kind of feature.
One thing that I haven't seen discussed is the fact that Java has had the
"const" keyword since the beginning, but it's never been implemented. That
raises the question of whether it's more practical (in terms of bridging
styles) for methods to guarantee that they don't change some object (i.e.
implement const) or for a data type to be declared in a way that it cannot
be mutated (i.e. the @Immutable discussion). Given the difficulties raised
already in attempts to create truly immutable lists, or perhaps read-only
lists, and merge them in the existing library APIs, perhaps it's worth
considering attacking the problem from the other side? Perhaps there's a
way to declare a particular reference as "only to be used with const /
non-mutating operations". Might the combination of those two features be
worth consideration?
Cheers,
Simon
On Mon, Nov 29, 2021 at 11:06 AM Alberto Otero Rodríguez <
albest512 at hotmail.com> wrote:
> I have created myself a project with a possible implementation of
> Immutable Collections using the source code of Java 17.
>
> https://github.com/Aliuken/JavaImmutableCollections
> [
> https://opengraph.githubassets.com/b1aeb5022614cc86e9ca7b0effe8a6298ce1ba62fee0b97021af3ff40e505cea/Aliuken/JavaImmutableCollections
> ]<https://github.com/Aliuken/JavaImmutableCollections>
> GitHub - Aliuken/JavaImmutableCollections<
> https://github.com/Aliuken/JavaImmutableCollections>
> Contribute to Aliuken/JavaImmutableCollections development by creating an
> account on GitHub.
> github.com
>
> ________________________________
> De: Alberto Otero Rodríguez <albest512 at hotmail.com>
> Enviado: jueves, 25 de noviembre de 2021 9:39
> Para: Justin Dekeyser <justin.dekeyser at gmail.com>
> Cc: core-libs-dev at openjdk.java.net <core-libs-dev at openjdk.java.net>
> Asunto: RE: Adding an @Immutable annotation to Java
>
> I have not thought about that. I'm not a Java expert.
>
> I just throwed the idea precisely to avoid "fake immutability", because a
> programmer could think one record is immutable simply by being a record,
> while this is false.
>
> There are probably lots of problems that need to be taken in consideration
> (like inheritance). But I just throw the idea because I think it would be a
> desirable feature.
>
> I would be grateful if some expert could deepen in possible problems and
> their solutions.
>
> Regards,
>
> Alberto Otero Rodríguez.
> ________________________________
> De: Justin Dekeyser <justin.dekeyser at gmail.com>
> Enviado: jueves, 25 de noviembre de 2021 9:27
> Para: Alberto Otero Rodríguez <albest512 at hotmail.com>
> Cc: core-libs-dev at openjdk.java.net <core-libs-dev at openjdk.java.net>
> Asunto: Re: Adding an @Immutable annotation to Java
>
> Hello,
>
> Quick question, out of curiosity: how would it behave with respect to
> inheritance? Can a @Immutable class inherit from an non immutable one?
> if no: that's a bit annoying, no? (fake immutability)
> Can @Immutable class be subclassed to a non @Immutable one? if no:
> that's a bit annoying too, no? (downcasting)
>
> Since Object is the super class of everything, it sounds like a
> problem. What have you thought about to handle this concern?
>
> Regards,
>
> Justin Dekeyser
>
> On Thu, Nov 25, 2021 at 9:08 AM Alberto Otero Rodríguez
> <albest512 at hotmail.com> wrote:
> >
> > Hi, I was thinking that it could be interesting adding an @Immutable
> annotation to Java. It would be a marker annotation for the compiler
> (similar to @FunctionalInterface) in order to throw an error if the
> annotated class/record has a component that is not @Immutable.
> >
> > This means that all existing immutable objects (like the
> primitive-wrapping objects and String) should be annotated with @Immutable
> and the programmer could, for example, annotate a new record object with
> @Immutable only if all its fields are annotated with @Immutable.
> >
> > What do you think?
> >
> > Regards,
> >
> > Alberto Otero Rodríguez.
>
--
Simon Roberts
(303) 249 3613
More information about the core-libs-dev
mailing list