From: lennart@augustsson.net Date: Sat, 20 Jan 2007 16:14:36 +0000 (+0000) Subject: Document the overloaded string extension. X-Git-Tag: 2007-02-05~39 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;ds=sidebyside;h=f94f26a7cb7bb7005b88f50c4383a761fc18718d;hp=90dc9026b091be5cca5da4c6cbd3713ecc493361;p=ghc-hetmet.git Document the overloaded string extension. --- 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. + + +