News for hackers. Stuff that matters.
Re:Paving the on-ramp to Java by hardware2win on Thursday September 29, 2022 @07:29AM (#33019085)
>like pure functions

Small nitpick :p

Re:Paving the on-ramp to Java by bheadmaster on Thursday September 29, 2022 @08:04AM (#33019402)
I think they meant "functions" in the mathematical sense.

Although due to the ambiguity of the word "function", your nitpick still has its place.

Re:Paving the on-ramp to Java by mcculley on Thursday September 29, 2022 @08:16AM (#33019503)
Yes, I definitely meant in the mathematical sense.

I would really like a way to express pure functions in Java. There would need to some way to mark objects as immutable at the language level.

Re:Paving the on-ramp to Java by kaashif on Thursday September 29, 2022 @08:48AM (#33019827)
I switched from being a C++ developer to Java a while back and the lack of const (i.e. immutability) annoys me.

It annoys me even more than no-one seems to care about it much, and most think final is good enough - you can still mutate final objects. In C++ all references are final (in a Java sense) since they can't be reseated, but const actually gets you immutability (modulo const_cast).

I just want everything to be immutable unless mutability is really truly needed!

Re:Paving the on-ramp to Java by kaba0 on Thursday September 29, 2022 @09:32AM (#33020377)
Clojure may be what you want then, if you can stomach the lisp-y syntax.

Otherwise Scala is pretty pragmatic about it (val vs var).

In both cases you still get only shallow immutability, but if nigh every object itself is immutable, shallow immutability may not be so shallow.

Re:Paving the on-ramp to Java by cobbzilla on Thursday September 29, 2022 @09:42AM (#33020497)
> I just want everything to be immutable unless mutability is really truly needed

Have you tried Rust? It’s nice in this regard; variables are immutable unless explicitly declared mutable.

Re:Paving the on-ramp to Java by kaashif on Saturday October 01, 2022 @11:31PM (#33052566)
Yeah, and I really like Rust. Rust has its mistakes but having RAII and immutability by default goes a very long way.

But it's not terribly easy or even a good idea to switch from Java to Rust at a real company that has no time to rewrite libraries, has to hire developers, and so on.

Re:Paving the on-ramp to Java by barbarbar on Saturday October 01, 2022 @12:42AM (#33043944)
What about Records and immutable Collections, Sets, Maps? Isn't it possible to achieve what you want with that?
Re:Paving the on-ramp to Java by kaashif on Saturday October 01, 2022 @11:24PM (#33052530)
Not really. In Java you can do all kinds of things with immutable wrappers, but the language doesn't help you and it doesn't come for free for your own types.

In C++ you can write a class with some methods that mutate, and some const methods that can't mutate anything, and you can pass around const references through which mutation is guaranteed to be impossible. This all comes for free.

Re:Paving the on-ramp to Java by barbarbar on Sunday October 02, 2022 @10:11AM (#33056005)
This was added a couple of years ago https://docs.oracle.com/en/java/javase/17/docs/api/java.base....

I was thinking that would make it possible to create unmodifiable data objects using that (and not Object as base class).