Closed som-snytt closed 7 months ago
since the computation may fail
and what are the consequences in that case?
It's an unnecessary allocation. People wonder in the context, "Can I save an allocation here?" as on the linked PR.
I'm looking at the mechanics of allocation on the jvm, where init is not completed.
I believe I heard that they are going to eliminate the new; dup; load args; invokespecial <init>
dance in a future Java release, but cannot find a reference right now.
In general, I don't think we need to optimize for this. If it was a measurable issue we would probably have heard about it by now. Also there's the "Java does the same" argument hammer.
Reproduction steps
Scala version: 2.13, 3.x
Problem
The by-name arg is evaluated after allocating (new). This is different from evaluating the arg before calling apply.
Strictly, the rewrite should pre-compute the arg to a temporary, since the computation may fail.
This doesn't matter much, but was noticed at https://github.com/scala/scala/pull/10600
Dotty constructor proxies work the same way.