The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

globulon (18) [Avatar] Offline
#1
Chap 4 classic constructor with a twist => default method as static ?
Houdy,

I was learning from chapter 4 when I read the compiler generates static method for obtaining the default value.
I ran with a test:


final class WithDefaultArgument {

def doSomething(withObject: String= "default"): Unit = {
println(withObject)
}

def run() {
doSomething()
}

}

and javap produced:

public final class WithDefaultArgument implements scala.ScalaObject {
public void doSomething(java.lang.String);
Code:
0: getstatic #11 // Field scala/Predef$.MODULE$:Lscala/Predef$;
3: aload_1
4: invokevirtual #16 // Method scala/Predef$.printlnsmilieLjava/lang/ObjectsmilieV
7: return

public java.lang.String doSomething$default$1();
Code:
0: ldc #25 // String default
2: areturn

public void run();
Code:
0: aload_0
1: aload_0
2: invokevirtual #31 // Method doSomething$default$1smilie)Ljava/lang/String;
5: invokevirtual #33 // Method doSomethingsmilieLjava/lang/StringsmilieV
8: return

public WithDefaultArgument();
Code:
0: aload_0
1: invokespecial #38 // Method java/lang/Object."<init>"smilie)V
4: return
}

where

public java.lang.String doSomething$default$1()

seems not to be static. Am I missing something ?
joshua.suereth (60) [Avatar] Offline
#2
Re: Chap 4 classic constructor with a twist =&gt; default method as static ?
Hi!

I should clarify those examples a bit. Default values, in the case of 'modules' or package-level objects, are static. However, for classes they are instance methods so that they can be polymorphic. I'll go update that description to make sure it's accurate.