Why not:
class isString1 a where fromString1 :: NonEmpty Char -> a
tl;dr
We have
instance IsString [Char]andinstance IsList (NonEmpty a). Let’s also haveIsString (NonEmpty Char).Background
IsStringis a class that is used with the-XOverloadedStringsextension to support string literals of types other thanString- for instance, with theIsString Textinstance in scope, you can write"foobar" :: Textand it will compile.For reference, the standard libraries supply the following instances of
IsString:instance (a ~ Char) => IsString [a] instance IsString a => IsString (Const a b) instance IsString a => IsString (Identity a)Proposal
I propose adding a new instance of
IsStringforNonEmptylists of characters.NonEmptyhas been inbasestarting from version 4.9, and for that reason people are starting to use it more often - e.g. the popularmegaparseclibrary defines its custom error type like this:data ErrorItem t = Tokens (NonEmpty t) -- ^ Non-empty stream of tokens | Label (NonEmpty Char) -- ^ Label (cannot be empty) | EndOfInput -- ^ End of inputHere
NonEmpty Charstands for “non-empty string”. Without theIsStringinstance users are forced to write non-empty strings in an inconvenient and awkward-looking way (e.g.Label ('f' :| "oobar")); the instance makesLabel "foobar"an acceptable notation, thus making theNonEmpty Chartype more viable for use in libraries and user-facing APIs.Implementation
Here’s a sample implementation:
instance (a ~ Char) => IsString (NonEmpty a) where fromString (a:as) = a :| as fromString "" = errorWithoutStackTrace "NonEmpty.fromString: empty string"This mirrors the
IsListinstance forNonEmpty. (The reason I haven’t usedfromListis that I want the error message to say “fromString” instead of “fromList”.)If this is accepted, I can make a patch.
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries