1.2 KiB
1.2 KiB
Haskell Type Classes and Custom Instances
This note covers 11-haskell-typeclasses/, which defines a custom type class and several instances that share one rendering interface.
1. Why Type Classes Matter
Type classes let you describe a capability independently from any one data type.
This example introduces:
Renderableas a capability,Environment,BuildState, andDeploymentas domain types, andDeploymentReportas a wrapper type for a whole collection.
Each type gets its own Renderable instance, but all of them can be used through the same render function.
2. Why the Instances Are Separate
Each instance decides how that type should appear:
Environmentrenders short environment names,BuildStaterenders status text, andDeploymentcombines those smaller renderings into one message.
That shows the core value of type classes: behavior is attached per type, while the calling code can stay generic.
3. Commands to Try
cd 11-haskell-typeclasses
nix develop
cabal run
cabal run -- production failed 2
cabal test
nix build
./result/bin/mini-render production failed 2
nix run . -- production failed 2
nix flake check