git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update typeclasses.rst
commit61aea87530daede27b0bb281f802e92623d0bb55 authorPhil Freeman <paf31@cantab.net> dateWed 23 Apr 2014, 22:45 (13 years ago) parent9d19f72
1 file changed+23−1
docs/source/typeclasses.rst+23−1
docs/source/typeclasses.rst+23−1
@@ -5,7 +5,10 @@ PureScript has basic support for type classes via the ``class`` and ``instance``
55
66Types appearing in class instances are must be of the form ``String``, ``Number``, ``Boolean``, or ``C t1 ... tn`` where ``C`` is a type constructor (including ``[]`` and ``->`` and ``t_i`` are types of the same form).
77
8−Type class instances are resolved based on the order in which they appeared in the source files. In particular, overlapping instances are permitted.
8+Type class instances are resolved based on the order in which they appeared in the source files. Overlapping instances will result in a compilation error.
9+
10+Example
11+-------
912
1013Here is an example of the ``Show`` typeclass, with instances for ``String``, ``Booleans`` and ``[]``::
1114
@@ -24,3 +27,22 @@ Here is an example of the ``Show`` typeclass, with instances for ``String``, ``B
2427 show (x:xs) = show x ++ " : " ++ showArray xs
2528
2629 example = show [true, false]
30+
31+Superclasses
32+------------
33+
34+Superclass implications can be indicated in a class declaration with a backwards fat arrow ``<=``::
35+
36+ class (Monad m) <= MonadFail m where
37+ fail :: forall a. String -> m a
38+
39+Superclass instances will be used when searching for an instance of a subclass. For example, in the code below, the ``Monad`` constraint introduced by the ``return`` function can be discharged since ``Monad`` is a superclass of ``MonadFail``.
40+
41+ assert :: forall m. (MonadFail m) => Bool -> m {}
42+ assert true = return {}
43+ assert false = fail "Assertion failed"
44+
45+Type Annotations
46+----------------
47+
48+A constrained type will never be inferred for a top-level declaration without an explicit type signature.