I'm new to Scala. I'd like to have a class which expresses something like coordinates measured in positive integers. (This is a simplified example; please don't point to the existence of other coordinate classes; assume that I really need a new class.)
My first attempt was to simply put require(x >= 0 && y >= 0)
into the class, but members of my team did not like that this would throw exceptions.
My second attempt returns an Option[Coordinate]
. So now every time I create such a coordinate, I have 4 lines where I would normally have just one. Worse, the Option
ality infects everything I'm building out these coordinates. Now I have an Option[Row]
, Option[Rectangle]
, Option[Cube]
, Option[Report]
...
Other people have advised me to fix the problem by making a Natural
number type which would always be positive. But now this pushes the issue to creating a Natural
out of a regular integer that might be negative.
I'm new to Scala, but it seems to me that Options, Either, and Try make sense if
(a) the operation can fail, because you're doing I/O
(b) there's a reasonable alternative to the absence of something
It seems that neither of these capture what I'm trying to do here, which is really to guard against someone making a mathematical mistake in their coding.
Advice?