Something small I have been working on to enhance basic types with type-safe wrappers to enforce invariants at compile time. Reading valid values from configuration was the initial driver for me to experiment with this.
I have just seen that @bvenners is working on something similar in Scalactic
- https://twitter.com/bvenners/status/529913792822394881
- https://github.com/scalatest/scalatest/tree/master/src/main/scala/org/scalactic/numbers
I really like his approach, here is a quick comparison with Scalactic's version
- it includes versions for Long, Float and Double; I just include Int
- it uses macros to ensure that basic construction only uses a valid Literal (this is an awesome idea!!!!)
- it uses Options where I use Try
- it provides sophisticated operator overloading for addition, I only provide basic operator support for percentage
- I have abstracted the companion object for wrapper types
- This is currently too verbose, I am still experimenting with different approaches here
- The WrapperValue.Companion provides a single location for implementing type class instances like Ordering and ConfigReader
- The WrapperValue.Companion provides an unapply extractor