From f94f26a7cb7bb7005b88f50c4383a761fc18718d Mon Sep 17 00:00:00 2001 From: "lennart@augustsson.net" Date: Sat, 20 Jan 2007 16:14:36 +0000 Subject: [PATCH] Document the overloaded string extension. --- docs/users_guide/flags.xml | 7 +++++ docs/users_guide/glasgow_exts.xml | 59 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/docs/users_guide/flags.xml b/docs/users_guide/flags.xml index 9c65282..eeb4f5d 100644 --- a/docs/users_guide/flags.xml +++ b/docs/users_guide/flags.xml @@ -662,6 +662,13 @@ + + Enable overloaded string literals. + + dynamic + + + Enable lexically-scoped type variables. Implied by . diff --git a/docs/users_guide/glasgow_exts.xml b/docs/users_guide/glasgow_exts.xml index 9e93fd7..0c3b07c 100644 --- a/docs/users_guide/glasgow_exts.xml +++ b/docs/users_guide/glasgow_exts.xml @@ -241,6 +241,14 @@ documentation describes all the libraries that come with GHC. + + + Enables overloaded string literals (see ). + + + + Enables lexically-scoped type variables (see + +Overloaded string literals + + + +GHC supports overloaded string literals. Normally a +string literal has type String, but with overloaded string +literals enabled (with -foverloaded-strings) + a string literal has type (IsString a) => a. + + +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. + + +The class IsString is defined as: + +class IsString a where + fromString :: String -> a + +And the only predefined instance is the obvious one to make strings work as usual: + +instance IsString [Char] where + fromString cs = cs + + + +A small example: + +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" + + + +Note that deriving Eq is necessary for the pattern matching +to work since it gets translated into an equality comparison. + + + -- 1.7.10.4