News for hackers. Stuff that matters.
Re:Java 21: No more public static void main by armchairhacker on Monday June 05, 2023 @12:23AM (#36192240)
Here are some other features to make Java more modern:

- Replace `System.out.println` with just `println`

- Add operator overloading and subscript overloading for lists and maps

- Add string templates with `$`, e.g. `"Hello ${user.name}!"`

- Add range types, e.g. `for (i in 1..10) { ... }`

- `val` keyword, like `var` but only doesn't let you assign to the variable after defining it

- Add coroutines

- Make `==` use `equals`, and define `===` for identity comparison

- Make types non-nullable by default, and use `?` to denote nullable types. Use `!` to cast a nullable type to non-nullable, and `?.` to access a field on a potentially nullable object

- Add readonly types for arrays and maps: replace `List` with `List` and `MutableList`, and `HashMap` with `Map` and `MutableMap`.

- Get rid of sigils for array types, use `Array` instead. Also get rid of lowercase and uppercase variants for primitive types, just use the uppercase ones for consistency

- Make it so that when a lambda is the last argument it can be outside of parentheses, when it's the only argument parentheses aren't needed, and when it has no arguments you can omit the `() =>`

- Put type annotations after the variable like in most modern languages, and use the `fun` keyword for functions. So instead of `void main(String[] args)` you would write `fun main(args: Array<String>)

- Put class constructor parameters on the class declaration, and allow them to be fields by specifying `var` or `val`, like `class Rectangle(var width: Double, var length: Double) {}`

- `init` blocks in classes for initialization code, and `constructor(sideLength: Double) : this(sideLength, sideLength)` for alternate constructors

- Add the `when` condition as a better replacement for `if else` chains.

- Add convenience functions to all objects: `x.let(fun)`, `x.also(fun)`, `x.apply(fun)`, `x.use(fun)`, and `x.apply(fun)`

I'm sure there are more improvements I haven't mentioned, but these would all be good first steps to make Java almost as good as other JVM languages like Kotlin

Re:Java 21: No more public static void main by Aaron2222 on Monday June 05, 2023 @12:28AM (#36192266)
That just sounds exactly like Kotlin?
Re:Java 21: No more public static void main by nightowl_games on Monday June 05, 2023 @01:05AM (#36192523)
The first few points seem like good first steps but you soon got to a land of "never gonna happen" and then you finally just landed on kotlin itself. Why not just use Kotlin?
Re:Java 21: No more public static void main by kaba0 on Monday June 05, 2023 @02:14AM (#36192998)
String templates are coming, there is IntStream.range(1, 10), there is `final var`, I don’t see much point to having coroutines with Loom, == would be ecosystem fracturing breaking change so please no, that nullability might actually come with the recent direction Valhalla’s value types took, readonly collections would be cool, but that ship has sailed, array sigils are such a small thing I don’t see the benefit, I don’t get the lambda one, variable: Type is again a breaking change for not good enough reason, records are immutable but once withers complete they will be imo better, you do have static blocks but with records you also get a canonical one with very similar syntax than what you want, if expressions were actually great, nested ternaries are the only syntax I really have a hard time with even after many years.

Kotlin is not the panacea of language design either.

Re:Java 21: No more public static void main by MagicMoonlight on Monday June 05, 2023 @02:39AM (#36193155)
The reason it’s System.out is because that’s one of the output channels. There are other output channels like System.err

Your suggestions are basically all just meme features from nobody languages. Java is the king for a reason.

Re:Java 21: No more public static void main by Asraelite on Monday June 05, 2023 @09:30AM (#36195936)
> Java is the king for a reason.

Of all the reasons Java is popular, ergonomics is absolutely not one of them.

Re:Java 21: No more public static void main by Aaron2222 on Monday June 05, 2023 @05:45PM (#36204311)
Kotlin is hardly a nobody language (being the default language for Android development these days). That being said, a lot of these are no-gos from a backwards compatibility perspective, and if you want them, I'm not sure why you wouldn't just use Kotlin.
Re:Java 21: No more public static void main by KRAKRISMOTT on Monday June 05, 2023 @03:09AM (#36193365)
Coroutines aside, you just re-invented Dart.
Re:Java 21: No more public static void main by mhoad on Monday June 05, 2023 @03:28AM (#36193497)
Came here to say exactly this. The language you are describing already exists. HN seems hellbent on pretending it hasn’t changed in 10 years but Dart was very much designed from the POV of what if we took the good parts from both Java and JS and left the bad bits behind and compiled it to native machine code. It’s actually a really good outcome that’s incredibly underrated.
Re:Java 21: No more public static void main by melony on Monday June 05, 2023 @07:59AM (#36195070)
My main gripe with Dart is their package manager. Unlike NPM or Cargo, it is possible to get conflicting diamond dependencies. The lack of shared memory concurrency is also disappointing.
Re:Java 21: No more public static void main by Hroble on Monday June 05, 2023 @05:20AM (#36194118)
That is the joke. Though probably Kotlin, not Dart, as the features described are exactly what Kotlin has.
Re:Java 21: No more public static void main by haspok on Monday June 05, 2023 @03:49AM (#36193645)
You forgot about backwards compatibility, which is very high on the Java features priority list. Things like "Make `==` use `equals`" will NEVER happen in Java.

Many of you other points are also purely stylistic (eg. types before or after parameter/variable names), these will not change just because you don't like it.

Re:Java 21: No more public static void main by okeuro49 on Monday June 05, 2023 @06:35AM (#36194557)
For coroutines look at structured concurrency https://openjdk.org/jeps/437
Re:Java 21: No more public static void main by revskill on Monday June 05, 2023 @07:13AM (#36194791)
What does structure concurrency mean ? What are other kinds of concurrency ?
Re:Java 21: No more public static void main by ohgodplsno on Monday June 05, 2023 @07:38AM (#36194935)
This is good reading on the subject: https://vorpus.org/blog/notes-on-structured-concurrency-or-g...

There's a single other kind of concurrency: unstructured concurrency. Which is where you just start threads/goroutines/coroutines/etc and they just... all do their own little thing ? They can return at any time, have no parents, etc.

Structured concurrency is about making a tree of concurrent processes. You may have a parent job, that has children. Cancelling this job will cancel all its children. For example, you may have a job that's linked to an UI (so it needs to stop the moment that UI is gone), to a socket (so it needs to stop the moment the socket closes), or to nothing and is a global scope (that will keep running as long as your OS doesn't exit the process). These scopes can in turn have their own children and their own scopes. And maybe you have a task way, way down in your tree that spawns two tasks: one is a long running task that must exit the moment its parent stops ("the user clicked on cancel"), one is a long running task that must stay alive as long as necessary ("the user's data is being uploaded to a server in the background"). With unstructured concurrency, you can't really express that. With structured concurrency, just pass the appropriate scope down.

Re:Java 21: No more public static void main by melony on Monday June 05, 2023 @08:00AM (#36195086)
What do also and use do?
Re:Java 21: No more public static void main by Aaron2222 on Monday June 05, 2023 @05:44PM (#36204281)
Kotlin has a bunch of scope functions[0]. `a.also(f)` takes a function `f`, calls `f(a)`, and returns `a`.

[0]: https://kotlinlang.org/docs/scope-functions.html

Re:Java 21: No more public static void main by exabrial on Monday June 05, 2023 @12:15PM (#36198501)
I'd vote no on most of these for Java language itself; it's supposed to be a C-like language, not a Ruby-like language. However, the jvm doesn't care what the source language is, it just cares about bytecode. These belong in a Kotlin or other language.
Re:Java 21: No more public static void main by Aaron2222 on Monday June 05, 2023 @05:50PM (#36204388)
A lot of these are no-gos anyway from a backwards compatibility perspective (many of which being entirely syntactical), and they're already in Kotlin, so I'm not sure why you wouldn't just use that. A couple I could potentially see making their way into Java though.
Re:Java 21: No more public static void main by exabrial on Monday June 05, 2023 @11:19PM (#36207630)
Exactly my thoughts. Let Java be Java. It's not that I disagree with most of them I just don't think it needs to have every bell, whistle, and doodad either. Simplicity is beauty.
Re:Java 21: No more public static void main by Aaron2222 on Tuesday June 06, 2023 @02:16AM (#36208836)
No point completely reshaping Java into something that's already on the JVM anyway.