No, I don’t mean lazy vals in the argument list. Why would anyone do that? I am referring to lazy vals as members of the class. As a basic example, I have a class that I call “LineSeg” to represent a finite line segment. It is determined by its end points, which are the constructor arguments. But it also has other derived properties, including it’s length and direction. These are not particularly costly to compute, but the cost could add up if they appear inside a loop that executes many times. The problem is that it is hard to know in advance how many instances of the class need those properties and how many access them many times. See the code snippet below
case class LineSeg(point1: Position, point2: Position) {
// finite 2D line segment
lazy val length = point1 separation point2
lazy val dir = point1 directionTo point2
lazy val alongDir = point1.unitVectorTo(point2) // unit vector in direction of segment
lazy val crossDir = Position(alongDir.y, -alongDir.x)