It is not possible to evaluate a local lazy val in Scala 3 (probably also in Scala 2) because the local variable is not visible in the class file or stack frame.
Example:
package example
object Main {
def main(args: Array[String]): Unit = {
lazy val msg = "Hello, World!"
=> println(msg)
}
}
$ msg
Cannot evaluate because of failed compilation:
-- Error: <expression>:1:0 -----------------------------------------------------
1 |msg
|^^^
|Evaluation of local lazy val not supported: val msg.
Details
The compiler tree of the main method afther phase genBCode is:
def main(args: String[]): Unit =
{
lazy var msg$lzy1: scala.runtime.LazyRef = new scala.runtime.LazyRef()
println(this.msg$1(msg$lzy1))
}
But msg$lzy1 is not visible in the stack frame nor in the class file:
$ javap -l example.Main$
Compiled from "Main.scala"
public final class example.Main$ implements java.io.Serializable {
public static final example.Main$ MODULE$;
public static {};
LineNumberTable:
line 4: 0
public void main(java.lang.String[]);
LineNumberTable:
line 4: 0
line 5: 0
line 6: 8
LocalVariableTable:
Start Length Slot Name Signature
0 20 0 this Lexample/Main$;
0 20 1 args [Ljava/lang/String;
To fix this we probably need to take action in the compiler itself.
Problem
It is not possible to evaluate a local lazy val in Scala 3 (probably also in Scala 2) because the local variable is not visible in the class file or stack frame.
Example:
Details
The compiler tree of the
main
method afther phasegenBCode
is:But
msg$lzy1
is not visible in the stack frame nor in the class file:To fix this we probably need to take action in the compiler itself.