4 ms·
A typeclass is really nothing but a basic java/c# interface. The 4 things you do with an interface are: 1. Declare an interface 2. Describe the contract
by BoiledCabbage 10y ago
A typeclass is really nothing but a basic java/c# interface.
The 4 things you do with an interface are:
1. Declare an interface
2. Describe the contract of the interface
3. Declare that a datatype (or class) adheres to that interface
4. Provide an implementation of the the contract of the interface (fulfill the contract).
A haskell Typeclass does exactly that. The keyword "class" is where you do #s 1 & 2. The keyword "instance" is where you do #'s 3 & 4.
The only notable difference is:
If an ISizable has int GetSize() method in haskell instead of the contract being int ISizable.GetSize(), it is static int GetSize(ISizable x).
Or in Haskell:
instance ISizable MyType where
GetSize :: ISizable -> Int -- Note you don't actually type this line
GetSize x = ...