Commit f94f26a7 authored by lennart@augustsson.net's avatar lennart@augustsson.net
Browse files

Document the overloaded string extension.

parent 90dc9026
......@@ -661,6 +661,13 @@
<entry>dynamic</entry>
<entry><option>-fno-extended-default-rules</option></entry>
</row>
<row>
<entry><option>-foverloaded-strings</option></entry>
<entry>Enable <link linkend="overloaded-strings">overloaded string literals</link>.
</entry>
<entry>dynamic</entry>
<entry><option>-fno-overloaded-strings</option></entry>
</row>
<row>
<entry><option>-fscoped-type-variables</option></entry>
<entry>Enable <link linkend="scoped-type-variables">lexically-scoped type variables</link>.
......
......@@ -240,6 +240,14 @@ documentation</ulink> describes all the libraries that come with GHC.
</listitem>
</varlistentry>
<varlistentry>
<term><option>-foverloaded-strings</option></term>
<listitem>
<para>Enables overloaded string literals (see <xref
linkend="overloaded-strings"/>).</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-fscoped-type-variables</option></term>
<listitem>
......@@ -4056,6 +4064,57 @@ pattern binding must have the same context. For example, this is fine:
</para>
</sect2>
<sect2 id="overloaded-strings">
<title>Overloaded string literals
</title>
<para>
GHC supports <emphasis>overloaded string literals</emphasis>. Normally a
string literal has type <literal>String</literal>, but with overloaded string
literals enabled (with <literal>-foverloaded-strings</literal>)
a string literal has type <literal>(IsString a) => a</literal>.
</para>
<para>
This means that the usual string syntax can be used, e.g., for packed strings
and other variations of string like types. String literals behave very much
like integer literals, i.e., they can be used in both expressions and patterns.
If used in a pattern the literal with be replaced by an equality test, in the same
way as an integer literal is.
</para>
<para>
The class <literal>IsString</literal> is defined as:
<programlisting>
class IsString a where
fromString :: String -> a
</programlisting>
And the only predefined instance is the obvious one to make strings work as usual:
<programlisting>
instance IsString [Char] where
fromString cs = cs
</programlisting>
</para>
<para>
A small example:
<programlisting>
newtype MyString = MyString String deriving (Eq, Show)
instance IsString MyString where
fromString = MyString
greet :: MyString -> MyString
greet "hello" = "world"
greet other = other
main = do
print $ greet "hello"
print $ greet "fool"
</programlisting>
</para>
<para>
Note that deriving <literal>Eq</literal> is necessary for the pattern matching
to work since it gets translated into an equality comparison.
</para>
</sect2>
</sect1>
<!-- ==================== End of type system extensions ================= -->
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment