O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Value Classes

Code like 2 days ago is charming, but what’s the catch? Let’s take a look under the hood to see what the Scala compiler actually did to that expression. Compile the file DateUtil.scala using scalac and run javap -c UseDateUtil\$, the shadow inner class that Scala created for the singleton, and quickly search for the call to the days method. Let’s examine a few lines of bytecode around the method call:

 
5: invokevirtual #73 // Method
 
DateUtil$.DateHelper:(I)LDateUtil$DateHelper;
 
8: getstatic #69 // Field
 
DateUtil$.MODULE$:LDateUtil$;
 
11: invokevirtual #77 // Method
 
DateUtil$.ago:()Ljava/lang/String;
 
14: invokevirtual #83 // Method
 
DateUtil$DateHelper.days:(Ljava/lang/String;)Ljava/time/LocalDate;

Here’s the cost of the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required