Would it be possible to find a way to expunge the keyword new
from scala 3?
There are two reasons why I’d like to do this. Firstly, there are a bunch of situations where you use or don’t use new
in fairly ad-hoc ways when introducing implementations that are allocated there and then. For example, but in no way limited to:
object foo extends Foo { ... }
lazy val bar = new Bar { ... }
or during callbacks:
foo.callWithCallback(new { override def onSuccess ... ; override def onFailure ... }
Once the magic of inlining and stack allocation and all the rest of it has taken place, the new
is often a lie. No object was actually allocated on the heap. Indeed, potentially there was never even an implementing class to instantiate.
The other place is when creating a new instance of a non-case class.
val c = new java.awt.Color(128,0,255)
This leads to various syntactic irregularities if you go on to call methods on this instance directly. It wouldn’t be unimaginable to have dotty generate the glue to allow us to say:
Color(128, 0, 255)
and proxy that through to the equivalent of new
in Java.
I think this requires a re-working of how blocks are dealt with. The rule would be something along the line of:
{ ... } : A
Given this block, look at the last expression. If it conforms to A
then treat it as an expression block to be evaluated. If it does not, look for member declarations needed for A
. If it looks like an attempt at implementing A, treat it as such.