Hacker News new | ask | show | jobs
by chriswarbo 2125 days ago
I introduced phantom types to a codebase recently, although it was Scala rather than Haskell. The code was using arrays of bytes, where:

- Some contain plaintext, some contain ciphertext

- Some contain keys, some contain data

- The keys themselves exist in plaintext and ciphertext forms (data keys generated by AWS KMS)

- Some keys were used for signing/verifying, some were used for encrypting/decrypting

- Some arrays contain Base64 data (for embedding in JSON), some contain unencoded data

Using 'Array[Byte]' would work, but would be error-prone. Using distinct types or wrappers like 'Base64[Plaintext[Key[Signing]]]' would require lots of extra definitions, and wrapping/unwrapping scattered around the code. Phantom types let me use type signatures with the right amount of specificity and polymorphism as needed, whilst the code itself was the 'straightforward' version without wrapping/unwrapping.