codesnippets:typeclasses
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| codesnippets:typeclasses [2021/04/10 16:52] – f2b216 | codesnippets:typeclasses [2025/10/08 00:48] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Type classes and its instances ====== | + | ====== Type classes and its instances, and the deriving mechanism |
| - | ~~DISCUSSION~~ | + | |
| * A type class (keyword: '' | * A type class (keyword: '' | ||
| Line 6: | Line 5: | ||
| * have instances (keyword: '' | * have instances (keyword: '' | ||
| * module '' | * module '' | ||
| - | * {{:codesnippets: | + | * {{:modules: |
| + | * picture from: Dirk Hünniger | ||
| + | * source: [[https:// | ||
| * type '' | * type '' | ||
| * below all types that have implemented instances | * below all types that have implemented instances | ||
| Line 14: | Line 15: | ||
| *'' | *'' | ||
| * '' | * '' | ||
| + | * additionally, | ||
| + | * {{: | ||
| + | * source: [[https:// | ||
| * example, implementing an '' | * example, implementing an '' | ||
| * functions '' | * functions '' | ||
| Line 21: | Line 25: | ||
| main :: IO () | main :: IO () | ||
| - | main = | + | main = putStrLn (show x1) |
| - | do | + | |
| - | print x1 | + | |
| class Enum a where | class Enum a where | ||
| Line 53: | Line 55: | ||
| 2 | 2 | ||
| </ | </ | ||
| - | * example, implementing a '' | + | |
| + | * function '' | ||
| + | * code:< | ||
| + | import Prelude hiding (Show(..)) | ||
| + | |||
| + | main :: IO () | ||
| + | main = putStrLn (show x1) | ||
| + | |||
| + | class Show a where | ||
| + | show :: a -> String | ||
| + | |||
| + | data MyInt = C0 | C1 | C2 | C3 | ||
| + | |||
| + | instance Show MyInt where | ||
| + | show C0 = " | ||
| + | show C1 = " | ||
| + | show C2 = " | ||
| + | show C3 = " | ||
| + | |||
| + | x1 = C3 | ||
| + | </ | ||
| + | * executes, with output:< | ||
| + | C3 | ||
| + | </ | ||
| + | * example, implementing an '' | ||
| + | * function '' | ||
| + | * code:< | ||
| + | main :: IO () | ||
| + | main = putStrLn (show x1) | ||
| + | |||
| + | data MyInt = C0 | C1 | C2 | C3 | ||
| + | |||
| + | instance Show MyInt where | ||
| + | show C0 = " | ||
| + | show C1 = " | ||
| + | show C2 = " | ||
| + | show C3 = " | ||
| + | |||
| + | x1 = C3 | ||
| + | </ | ||
| + | * executes, again with output:< | ||
| + | C3 | ||
| + | </ | ||
| + | |||
| + | ===== Deriving mechanism ===== | ||
| + | |||
| + | Looking to the examples above you may wonder, whether there an easier way to implement an '' | ||
| + | |||
| + | * example, where a data type applies '' | ||
| + | * to implement all function of typeclass '' | ||
| + | * code:< | ||
| + | main :: IO () | ||
| + | main = putStrLn (show x1) | ||
| + | |||
| + | data MyInt = C0 | C1 | C2 | C3 | ||
| + | deriving (Show, Enum) | ||
| + | |||
| + | x1 = succ C1 | ||
| + | </ | ||
| + | * executes, again with output:< | ||
| + | C2 | ||
| + | </ | ||
| + | |||
| + | ===== Advanced type classes ===== | ||
| + | |||
| + | | ||
| + | * code:<code Haskell> | ||
| import Prelude hiding (Maybe(..), Functor(..)) | import Prelude hiding (Maybe(..), Functor(..)) | ||
| Line 77: | Line 145: | ||
| </ | </ | ||
| + | |||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/typeclasses.1618066325.txt.gz · Last modified: (external edit)
