[SOLVED] Overflow examples from "Programming in Scala 5th Edition"

Section 22.4 “Using a typeclass”, page 484 has overflow examples, I put them into Scastie: Scastie - An interactive playground for Scala.

Which is correct? Could not find anything in the book’s Errata.
Possibly an error in the book?

So, for -Short.MinValue and -Byte.MinValue the overflows do not happen,
due to, I’m guessing, an implicit conversion to Int.

But with Short.MinValue.abs and Byte.MinValue.abs the overflows DO happen.
Why? Why no conversion in that case? Probably because unary_- is overloaded
differently than abs?

That is indeed an error in the book. It isn’t an implicit conversion to Int, though. Int is the result type of unary_- on Byte and Short:

Please add it to the errata if you have time.




1 Like