essential-scala icon indicating copy to clipboard operation
essential-scala copied to clipboard

fix bug in solution 4.3: use def not val in trait

Open russHyde opened this issue 5 years ago • 0 comments

Exercise 4.1.4.3 Shaping Up 2 (Da Streets) had a bug. Using the code as it existed meant that

new Square(2.0).perimeter

was 0.0 (instead of 8.0)

This occurred because the override val perimeter = 2 * ... was evaluated at the definition of trait Rectangular, rather than after creation of a new Square object. The definition of perimeter and area should have used def or lazy val; the preceding text recommended using def within traits. Hence the change.

russHyde avatar Jan 20 '19 20:01 russHyde