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
8: getstatic #69 // Field
11: invokevirtual #77 // Method
14: invokevirtual #83 // Method

Here’s the cost of the ...

Get Pragmatic Scala now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.