
On 3 September 2017 at 19:33, Tony Morris
Why not:
class isString1 a where fromString1 :: NonEmpty Char -> a
It's backwards-incompatible, but my preference - especially when you consider the libraries that parse the String - is to have fromString return Maybe.
On 02/09/17 22:08, Artyom wrote:
tl;dr
We have instance IsString [Char] and instance IsList (NonEmpty a). Let’s also have IsString (NonEmpty Char).
Background
IsString is a class that is used with the -XOverloadedStrings extension to support string literals of types other than String - for instance, with the IsString Text instance in scope, you can write "foobar" :: Text and 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 IsString for NonEmpty lists of characters. NonEmpty has been in base starting from version 4.9, and for that reason people are starting to use it more often - e.g. the popular megaparsec library 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 input
Here NonEmpty Char stands for “non-empty string”. Without the IsString instance users are forced to write non-empty strings in an inconvenient and awkward-looking way (e.g. Label ('f' :| "oobar")); the instance makes Label "foobar" an acceptable notation, thus making the NonEmpty Char type 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 IsList instance for NonEmpty. (The reason I haven’t used fromList is 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
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com