Hi,
Feeling crippled for too long by value classes not being able to nest I've ventured into the compiler codebase to see if I can lift this restriction.
I'm looking for advice and help, currently I'm trying to get this to run (test/files/run/t7685-class-simple.scala):
final class Foo(val x: String) extends AnyVal { override def toString = "" + x }
final class Bar(val f: Foo) extends AnyVal { override def toString = "" + f }
def main(args: Array[String]) = {
but it's failing in the constructor of Bar:
java.lang.ClassCastException: Test$Foo cannot be cast to java.lang.String
at Test$.main(t7685-class-simple.scala:7)
at Test.main(t7685-class-simple.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
I can get a little more insight from decompiling in the REPL - compared to Foo, creating a Bar has an additional checkcast call which I assume is causing the ClassCastException:
[info] Running scala.tools.nsc.MainGenericRunner -usejavacp
Welcome to Scala 2.12.0-20160509-125434-7d0738b (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_74).
Type in expressions for evaluation. Or try :help.
// Entering paste mode (ctrl-D to finish)
final class Foo(val x: String) extends AnyVal
final class Bar(val f: Foo) extends AnyVal
def bar = new Bar(new Foo("abc"))
// Exiting paste mode, now interpreting.
scala> :javap -c Create$
Compiled from "<pastie>"
public final class Create$ {
public static final Create$ MODULE$;
public static {};
Code:
0: new #2 // class Create$
3: invokespecial #12 // Method "<init>":()V
6: return
public java.lang.String foo();
Code:
0: ldc #16 // String abc
2: areturn
public java.lang.String bar();
Code:
0: new #20 // class Foo
3: dup
4: ldc #16 // String abc
6: invokespecial #23 // Method Foo."<init>":(Ljava/lang/String;)V
9: checkcast #25 // class java/lang/String
12: areturn
}
Where and how can I change the bytecode that is emitted? What's a good technique for TDD/iterating my way to something that seems to work?
In truth, any advice is very welcome, I've no idea what I'm doing :)
Thanks,
Dale