git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Create typeclasses.rst
3608e18Phil Freeman
docs/source/typeclasses.rstnew file+29
@@ -0,0 +1,29 @@
1+## Type Classes
2+
3+PureScript has basic support for type classes via the `class` and `instance` keywords.
4+
5+Type class instances must be of the form `String`, `Number`, `Boolean`, `[t]` or `C t1 ... tn`, and only a single type class parameter is supported.
6+
7+Type class instances are resolved based on the order in which they appeared in the source files. In particular, overlapping instances are permitted.
8+
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+```