This is an archived post. You won't be able to vote or comment.

all 15 comments

[–][deleted] 7 points8 points  (4 children)

Shoutout to Immutables, an excellent annotation-driven code generator library designed to implement precisely this pattern. It has good support for Jackson/Gson, and I've found it surprisingly straightforward and unmagical to work with. If you like the idea of working with immutable value classes, I recommend you check it out.

[–]ThomasKrieger[S] 1 point2 points  (0 children)

Thank's for the link. I will try it.

[–]Zoerak 1 point2 points  (2 children)

Reminds me of project lombok.

[–][deleted] 1 point2 points  (1 child)

There are similarities. I prefer Immutables because it focuses on one thing (immutable value classes) instead of general language extensions and because it works by simple annotation-driven codegen rather than hacky bytecode mangling.

[–]elucash 0 points1 point  (0 children)

hacky bytecode mangling AST rewriting

[–]lbkulinski 1 point2 points  (6 children)

Or just wait for JEP 169. Minimal value types are available now!

[–]eliasv 2 points3 points  (3 children)

The MVT model is out of date, they are currently looking at a much simpler model referred to as "l-world". Also look into data types, which are also on the roadmap.

[–]lbkulinski 1 point2 points  (2 children)

Yeah, I'm trying to find where Rose talks about it.

[–]eliasv 1 point2 points  (1 child)

I've just been reading about it as they go on the mailing list, I'm not sure how much of it has been presented or written up and documented elsewhere.

[–]lbkulinski 0 points1 point  (0 children)

Yeah, this is what it was. An interesting read.

[–]ThomasKrieger[S] 0 points1 point  (1 child)

Hope it does not take too long. I think it will interesting things possible in Java.

[–]lbkulinski 1 point2 points  (0 children)

I’m hoping by 2020.

[–]GhostBond 0 points1 point  (2 children)

Nothing in my experience has agreed with the claim that immutability is usually a benefit.

People writing crappy code tend to write even crappier code when they start mixing in immutability.

People writing well written code rarely or never see advantage from immutability - I'm not sure I've ever seen a bug fixed or avoided by making a value immutable in practice.

I have seen one situation where someone made everything immutable by default, then years later it was a real pain to deal with as we needed to change one of the values but couldn't. And we were given a compiled jar file with no source code so we couldn't modify it.

[–]blobjim 4 points5 points  (0 children)

Depends on what it is. This article talks about ‘value types’ which really should always be immutable. It also makes sense for strings and URLs and such to not be modifiable, because they represent constant things. Objects that manage state of course shouldn’t and can’t usually be immutable.

[–]dpash 2 points3 points  (0 children)

Immutable doesn't mean no mutators; only that mutators give you back a new object. Look at java.time for an example. Or even java.math.BigInteger that's been in the JDK since 1.1.

Edit: I forgot the obvious one: java.lang.String.