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 ...

Get Pragmatic Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.