overloaded num types, but not string types?
Wolfgang Jeltsch
wolfgang@jeltsch.net
26 Jul 2002 20:24:15 +0200
On Wednesday, 2002-07-24, 20:29, CEST, Hal Daume III wrote:
> I was wondering if anyone's thought of overloading string literals in
> the same way that numeric literals are overloaded. I know that I tend
> to use PackedStrings for almost everything, primarly due to the RegExp
> stuff and efficiency. This means my code is littered with "unpackPS"
> and "packString", as in
>
> ... if foo == packString "bar" ...
>
> or
>
> ... if unpackPS foo == "bar" ...
>
> I was wondering if someone might consider overloading Strings, too.
> Something like:
>
> class StringLiteral s where
> fromString :: String -> s
> toString :: s -> String -- not necessary, really
>
> then literals in haskell source could be converted from
>
> ... "foo" ...
>
> to
>
> ... (fromString "foo") ...
>
> we'd have instances like
>
> instance StringLiteral String where fromString = id
> instance StringLiteral PackedString where fromString = packString
> ...etc...
>
> if not something in ghc, perhaps this might find its way into drift???
>
> - hal
> [trailer]
Hi,
I already have a similar idea for characters. In my Seaweed library, I
have a lot of types which describe different subsets of the Unicode
character set. Examples are ASCIIChar, PASCIIChar (for printable ASCII
characters), and GASCIIChar (for graphical ASCII characters) but also
types for domain specific character sets. Each of these types is an
instance of a class Character which has the two methods toChar and
fromChar.
I would be happy if I could denote values of Character instances by just
using character literals, and if I could pattern-match against such
values. (Maybe, for the latter thing, one needs the conversion back to
the standard type.)
But my idea goes beyond this. I thought of some kind of a "character
type definition" --- something like
charactertype ASCIIControlChar = '\NUL' ..'\US' | '\DEL'
--- which introduces a character type and automatically instantiates
several classes (e.g. Eq, Ord, Show, Read and, of course, Character) in
a manner suitable for character types. This would save me a lot of time
of writing all the similar looking instance declarations.
(By the way, StringLiteral would be an improper name for your class
because instances of the class, like PackedString, describe strings and
not string literals.)
Wolfgang