Three hundred and twenty years have passed since the coven sank in the dark.
Removes the magic from witch. This provides safe conversions like witch does. But it doesn't use type classes or exceptions. This has a couple of advantages:
- No need to use type application for function selection.
- Functions get names that describe what they do. This allows ctags to work as well.
- No trouble with orphans.
- Custom errors instead of the prelude based ones allow client code to recover with typesafety even on partial conversions.
The idea is to select some module from which type you want to convert. So if you have a double you do:
import qualified Unwitch.Convert.Double as Double
Next you simply use the functions within the module to convert to something you want, for example:
spec = do
Double.toFloat 5.6 `shouldBe` 5.6Although dubious since float holds less information then double, this can never fail in the current implementation. But there are situation where conversion can fail, such as when converting to an integer:
spec = do
Double.toInteger 5.0 `shouldBe` Right 5
Double.toInteger 5.6 `shouldBe` Left $ RationalConversion $ DenomNotOne (5 % 6)In these situation you'll Right for success, or Left
for an error.
The library will attempt to give you as much information
as possible,
in this case the denominator wasn't 1 so the rational
conversion failed.
Type applications are unnecessary because there is no polymorphism. Since there are no polymorphic types we don't need to distinct between conversions that fail and conversions that don't, This is encoded in types. Furthermore on failure, if the client can figure out how to go on with a Rational, they can. In witch this information gets squashed, we happily provide it. It's not an exception like witch implied.
Enter the nix shell.
nix-shell
You can checkout the makefile to see what's available:
cat makefile
make run
make ghcid
- Modules names indicate where you're converting from.
- Errors should be packed with information to allow recovery
- Only restricted to primitives in base. The original witch had support for Map, and Text. I feel this is out of scope.