As we saw, case classes significantly simplify handling of new nested data structures that we want to construct. The case class definition is probably the most convincing reason to move from Java (and SQL) to Scala. Now, what about the methods? How do we quickly add methods to a class without expensive recompilation? Scala allows you to do this transparently with traits!
A fundamental feature of functional programming is that functions are a first class citizen on par with objects. In the previous section, we defined the two EpochSeconds
functions that transform the ISO8601 format to epoch time in seconds. We also suggested the splitSession
function that provides a multi-session view for a given IP. How do we associate this or other behavior with a given class?
First, let's define a desired behavior:
scala> trait Epoch { | this: PageView => | def epoch() : Long = { LocalDateTime.parse(ts, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toEpochSecond...