Scala 3 makes reserved word "new" optional
Briefly

Scala 3 makes reserved word "new" optional
"As a toy example, consider package org.example.graphicsobject WrappedColor { def apply(r: Int, g: Int, b: Int): WrappedColor = new WrappedColor(r, g, b)}import java.awt.Colorclass WrappedColor(r: Int, g: Int, b: Int) { val color = new Color(r, g, b)} Then we can write things like val wrapC = WrappedColor(128, 0, 255) Okay, seems like a lot of effort just to not have to write " new." But Scala programmers with a lot more sophistication than myself have been wanting this effort."
"I have been aware of the push to eliminate " new" from Scala 3 since 2019. Matthew Pocock posted to the Scala contributors forum his desire to "expunge" " new " from Scala 3. One of the examples he gives is about rewriting val c = new Color(128, 0, 255) as val c = Color(128, 0, 255) where Color is defined in the java.awt package. You can't do that in Scala 2. The error message in Scala 2 is the rather unhelpful "cannot resolve symbol.""
Scala 3 no longer requires the reserved word 'new' for constructor calls, even for Java-defined classes. Upgrading Scala 2 projects or starting Scala 3 projects in IntelliJ IDEA may present practical difficulties. The push to remove 'new' began around 2019 when Matthew Pocock proposed to 'expunge' the keyword and gave examples rewriting val c = new Color(128, 0, 255) as val c = Color(128, 0, 255). Scala 2 programmers used companion-object apply() methods as a workaround, shown by a WrappedColor example that returns new WrappedColor(...). Scala 3's compiler automates that effort, eliminating the need for manual apply wrappers.
Read at Medium
Unable to calculate read time
[
|
]