An IsString (NonEmpty Char) instance

Tony Morris tonymorris at gmail.com
Sun Sep 3 09:33:17 UTC 2017


Why not:

class isString1 a where fromString1 :: NonEmpty Char -> a


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 at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20170903/f866b09e/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20170903/f866b09e/attachment-0001.sig>


More information about the Libraries mailing list