User Tools

Site Tools


codesnippets:typeclasses

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
codesnippets:typeclasses [2021/04/10 20:07] f2b216codesnippets:typeclasses [2025/10/08 00:48] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Type classes and its instances, and the deriving mechanism ====== ====== Type classes and its instances, and the deriving mechanism ======
-~~DISCUSSION~~ 
  
   * A type class (keyword: ''class'') is a set of functions declarations that    * A type class (keyword: ''class'') is a set of functions declarations that 
Line 6: Line 5:
     * have instances (keyword: ''instance'') with type specific bindings.     * have instances (keyword: ''instance'') with type specific bindings.
   * module ''Prelude'' already consists of many type classes   * module ''Prelude'' already consists of many type classes
-    * {{:modules:preludetypeclasses.png?100|Type classes in module Prelude}}+    * {{:modules:preludetypeclasses.png?direct&300|}}
       * picture from: Dirk Hünniger       * picture from: Dirk Hünniger
       * source: [[https://commons.wikimedia.org/wiki/File:HaskellClasses.svg]]       * source: [[https://commons.wikimedia.org/wiki/File:HaskellClasses.svg]]
Line 16: Line 15:
         *'' Float''         *'' Float''
         * ''Double''         * ''Double''
 +  * additionally, there is many other libraries available with important type classes
 +    * {{:codesnippets:typeclassopedia-diagram.png?direct&700|}}
 +      * source: [[https://wiki.haskell.org/Typeclassopedia]]
   * example, implementing an ''Enum'' type class, and an instance for a data type   * example, implementing an ''Enum'' type class, and an instance for a data type
     * functions ''succ'', and ''pred'' have default implementations in ''class  Enum'',      * functions ''succ'', and ''pred'' have default implementations in ''class  Enum'', 
Line 99: Line 101:
 ===== Deriving mechanism ===== ===== Deriving mechanism =====
  
-Looking to the examples above you may wonder, whether there is a better way to implement an ''instance'' of standard type classes. Yes, there is the ''deriving'' mechanism.+Looking to the examples above you may wonder, whether there an easier way to implement an ''instance'' of standard type classes. Yes, there is the ''deriving'' mechanism.
  
   * example, where a data type applies ''deriving'' mechanism   * example, where a data type applies ''deriving'' mechanism
Line 118: Line 120:
 ===== Advanced type classes ===== ===== Advanced type classes =====
  
-  * example, implementing a ''Functor'':<code Haskell>+  * example, implementing a ''Functor'' 
 +    * code:<code Haskell>
 import Prelude hiding (Maybe(..), Functor(..)) import Prelude hiding (Maybe(..), Functor(..))
  
Line 142: Line 145:
 </code> </code>
  
 +
 +===== ✎ =====
 +~~DISCUSSION~~
codesnippets/typeclasses.1618078037.txt.gz · Last modified: (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki