3 ms·
Here are some other features to make Java more modern: - Replace `System.out.println` with just `println` - Add operator overloading and subscript overloading
by armchairhacker 3y ago
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
- Aaron2222 3y agoThat just sounds exactly like Kotlin?
- nightowl_games 3y agoThe 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?
- kaba0 3y agoString 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.
- MagicMoonlight 3y agoThe 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.
- Asraelite 3y ago> Java is the king for a reason. Of all the reasons Java is popular, ergonomics is absolutely not one of them.
- Aaron2222 3y agoKotlin 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.
- deleted 3y ago[deleted]
- KRAKRISMOTT 3y agoCoroutines aside, you just re-invented Dart.
- mhoad 3y agoCame 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.
- melony 3y agoMy 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.
- Hroble 3y agoThat is the joke. Though probably Kotlin, not Dart, as the features described are exactly what Kotlin has.
- haspok 3y agoYou 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.
- okeuro49 3y agoFor coroutines look at structured concurrency https://openjdk.org/jeps/437 https://openjdk.org/jeps/437
- revskill 3y agoWhat does structure concurrency mean ? What are other kinds of concurrency ?
- ohgodplsno 3y agoThis is good reading on the subject: https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/ 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.
- melony 3y agoWhat do also and use do?
- Aaron2222 3y agoKotlin 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 https://kotlinlang.org/docs/scope-functions.html
- exabrial 3y agoI'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.
- Aaron2222 3y agoA 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.