[Document the overloaded string extension. lennart@augustsson.net**20070120161436] { hunk ./docs/users_guide/flags.xml 664 + + + Enable overloaded string literals. + + dynamic + + hunk ./docs/users_guide/glasgow_exts.xml 243 + + + + Enables overloaded string literals (see ). + + + hunk ./docs/users_guide/glasgow_exts.xml 4065 + + + +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. + }