git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update typeclasses.rst
commit4efe14b2d260796837e7d7acb1a3072b047603a5 authorPhil Freeman <paf31@cantab.net> dateWed 22 Jan 2014, 23:02 (13 years ago) parent0237bc4
1 file changed+19−22
docs/source/typeclasses.rst+19−22
docs/source/typeclasses.rst+19−22
@@ -1,4 +1,5 @@
1−## Type Classes
1+Type Classes
2+============
23
34PureScript has basic support for type classes via the `class` and `instance` keywords.
45
@@ -6,24 +7,20 @@ Type class instances must be of the form `String`, `Number`, `Boolean`, `[t]` or
67
78Type class instances are resolved based on the order in which they appeared in the source files. In particular, overlapping instances are permitted.
89
9−```haskell
10−class Show a where
11− show :: a -> String
12−
13−instance Show String where
14− show s = s
15−
16−instance Show Boolean where
17− show true = "true"
18− show false = "false"
19−
20−instance (Show a) => Show [a] where
21− show arr = "[" ++ showArray arr ++ "]"
22−
23−showArray :: forall a. (Show a) => [a] -> String
24−showArray [] = ""
25−showArray [x] = show x
26−showArray (x:xs) = show x ++ ", " ++ showArray xs
27−
28−example = show [true, false]
29−```
10+Here is an example of the `Show` typeclass, with instances for Strings, Booleans and arrays::
11+
12+ class Show a where
13+ show :: a -> String
14+
15+ instance Show String where
16+ show s = s
17+
18+ instance Show Boolean where
19+ show true = "true"
20+ show false = "false"
21+
22+ instance (Show a) => Show [a] where
23+ show [] = "[]"
24+ show (x:xs) = show x ++ " : " ++ showArray xs
25+
26+ example = show [true, false]