https://jappieklooster.nl Jappiejappie Jappiejappie

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:

  1. No need to use type application for function selection.
  2. Functions get names that describe what they do. This allows ctags to work as well.
  3. No trouble with orphans.
  4. Custom errors instead of the prelude based ones allow client code to recover with typesafety even on partial conversions.

Usage

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.6

Although 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.

Comparison

Witch

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.

Hacking

Tools

Enter the nix shell.

nix-shell

You can checkout the makefile to see what’s available:

cat makefile

Running

make run

Fast filewatch which runs tests

make ghcid

Design decisions

  • 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.

https://serokell.io/blog/introduction-to-template-haskell

Changes

Change log for unwitch project

Version 2.0.0

  • fix cabal file links
  • delete complex module which made no sense.

Version 1.0.0

  • claude generate most conversion functions, looks good

Version 0.1.0

Implement some of Double. Write description

Version 0.0.0

import template.