git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update types.rst
commite7ab8ab6498186af378aa0391c67a8a08c55bb95 authorPhil Freeman <paf31@cantab.net> dateThu 31 Jul 2014, 21:25 (12 years ago) parentdce3cda
1 file changed+15
docs/source/types.rst+15
docs/source/types.rst+15
@@ -7,6 +7,7 @@ The type system defines the following types:
77- Arrays
88- Records
99- Tagged Unions
10+- Newtypes
1011- Functions
1112- Polymorphic Types
1213- Constrained Types
@@ -48,6 +49,20 @@ In the example, Foo is a tagged union type which has two constructors. It's firs
4849
4950``runFoo`` is an example of pattern matching on a tagged union type to discover its constructor, and the last line shows how to construct values of type ``Foo``.
5051
52+Newtypes
53+--------
54+
55+Newtypes are like data types introduced with the ``data`` keyword, but are restricted to a single contructor which contains a single arguments. Newtypes are introduced with the ``newtype`` keyword::
56+
57+ newtype Percentage = Percentage Number
58+
59+The representation of a newtype at runtime is the same as the underlying data type. For example, a value of type ``Percentage`` is just a JavaScript number at runtime.
60+
61+Newtypes can be assigned their own type class instances, so for example, ``Percentage`` can be given its own ``Show`` instance::
62+
63+ instance showPercentage :: Show Percentage where
64+ show (Percentage n) = show n ++ "%"
65+
5166Functions
5267---------
5368