By way of background, I recently coded a small app that detects the number of boxes in an image. The app defines some types, for example:
class Box(val x1: Int, val y1: Int, val x2: Int, val y2: Int)
This app is Kotlin, but the principles should apply to any strongly-typed language. The definition of
Box above is how I've typically seen code written. It works just fine. However, just using
Int for the type, we could inadvertently assign an X-coordinate, where Y-coordinate is expected. I realised I could use the type system to avoid such an error, by defining
class Box(val x1: XCoordinate, val y1: YCoordinate,
val x2: XCoordinate, val y2: YCoordinate)
In fact, when I did this I actually detected a bug - I'd copy&pasted code to detect horizontal lines to detect vertical lines, and forgotten to update one bit.
I figure this is not a novel technique, but I've not really come across it in practice, and without knowing what it's called, it's hard to Google. If you have information on what the technique is called, or even better, advice on how to do it well, that would be great!