String literals

Lennart Augustsson lennart at augustsson.net
Fri Nov 10 22:49:15 EST 2006


I think it's time that string literals got overloaded just like  
numeric literals.  There are several reasons for this.  One reason is  
the new fast string libraries.  They are great, but string literals  
don't work; you need to pack them first.  Another reason is the  
increasing use of Haskell for DSELs.  In a DSEL you might want string  
literals to have a different type than the ordinary String.

I have not implemented anything yet, but I would like to see  
something along the lines of the following:

class IsString s where
     fromString :: String -> s
instance IsString String where
     fromString = id

The instance declaration is not allowed in Haskell-98, but it can be  
rewritten as
class IsChar c where  -- Make this class local to it's defining module
     fromChar :: Char -> c
instance IsChar Char where
     fromChar = id
instance (IsChar c) => IsString [c] where
     fromString = map fromChar

And, like with numeric literals, any string literal will then have an  
implicit fromString insert to make the right conversion.

My guess is that the defaulting mechanism needs to be extended to  
default to the String type as well, or we'll get some ambiguous  
expressions.

Any thoughts?

	-- Lennart



More information about the Haskell-prime mailing list