diff options
author | lennart@augustsson.net <unknown> | 2007-01-20 16:14:36 +0000 |
---|---|---|
committer | lennart@augustsson.net <unknown> | 2007-01-20 16:14:36 +0000 |
commit | f94f26a7cb7bb7005b88f50c4383a761fc18718d (patch) | |
tree | 09907c906d07b092c5566aa3c0322449ccf0f5f0 /docs | |
parent | 90dc9026b091be5cca5da4c6cbd3713ecc493361 (diff) | |
download | haskell-f94f26a7cb7bb7005b88f50c4383a761fc18718d.tar.gz |
Document the overloaded string extension.
Diffstat (limited to 'docs')
-rw-r--r-- | docs/users_guide/flags.xml | 7 | ||||
-rw-r--r-- | docs/users_guide/glasgow_exts.xml | 59 |
2 files changed, 66 insertions, 0 deletions
diff --git a/docs/users_guide/flags.xml b/docs/users_guide/flags.xml index 9c65282dbd..eeb4f5dd47 100644 --- a/docs/users_guide/flags.xml +++ b/docs/users_guide/flags.xml @@ -662,6 +662,13 @@ <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>. Implied by <option>-fglasgow-exts</option>.</entry> diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml index 9e93fd73db..0c3b07cb6c 100644 --- a/docs/users_guide/glasgow_exts.xml +++ b/docs/users_guide/glasgow_exts.xml @@ -241,6 +241,14 @@ documentation</ulink> describes all the libraries that come with GHC. </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> <para>Enables lexically-scoped type variables (see <xref @@ -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 ================= --> |