From 7eb0ce0c49de737c108c4c446b628d32c3b8504f Mon Sep 17 00:00:00 2001 From: Veronika Romashkina Date: Thu, 14 May 2020 17:41:08 +0100 Subject: [PATCH] [#286] Consistent type for 'readMaybe' and 'readEither' (#296) * [#286] Consistent type for 'readMaybe' and 'readEither' Resolves #286 * Update CHANGELOG.md Co-authored-by: Dmitrii Kovanikov Co-authored-by: Dmitrii Kovanikov --- CHANGELOG.md | 19 +++++++++++++++++++ src/Relude/String.hs | 1 + src/Relude/String/Conversion.hs | 11 ++++++----- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 320e6dda..f3f702ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,25 @@ The changelog is available [on GitHub][2]. `Relude.List.Reexport`. * [#293](https://github.com/kowainik/relude/issues/293): Add `memptyIfFalse` and `memptyIfTrue` functions. +* [#286](https://github.com/kowainik/relude/issues/286): + __Breaking change:__ `readEither` is not polymorphic over the first argument + anymore. Now it takes `String`. + + __Migration rules:__ Use one of the conversion function from the + `Relude.String.Conversion` module to covert your old input value into + `String`. + + For example, if you had + + ```haskell + readEither @Text @Int myText + ``` + + Now it should become: + + ```haskell + readEither @Int (toString myText) + ``` ## 0.6.0.0 — Oct 30, 2019 diff --git a/src/Relude/String.hs b/src/Relude/String.hs index 24094caa..a8280695 100644 --- a/src/Relude/String.hs +++ b/src/Relude/String.hs @@ -55,4 +55,5 @@ Reexport data types and functions to work with 'Text', 'ByteString', -} {- $conversion Conversion functions between 'Text', 'String', 'ByteString'. +Also some read|show helper functions. -} diff --git a/src/Relude/String/Conversion.hs b/src/Relude/String/Conversion.hs index 2248afd8..e1b0ffa1 100644 --- a/src/Relude/String/Conversion.hs +++ b/src/Relude/String/Conversion.hs @@ -482,15 +482,16 @@ type family EncodingError ':<>: 'Text " -> Either UnicodeException " ':<>: 'Text to ) -{- | Polymorhpic version of 'Text.Read.readEither'. +{- | Version of 'Text.Read.readEither' that returns 'Text' in case of the parse +error. ->>> readEither @Text @Int "123" +>>> readEither @Int "123" Right 123 ->>> readEither @Text @Int "aa" +>>> readEither @Int "aa" Left "Prelude.read: no parse" -} -readEither :: (ToString a, Read b) => a -> Either Text b -readEither = first toText . Text.Read.readEither . toString +readEither :: (Read a) => String -> Either Text a +readEither = first toText . Text.Read.readEither {-# INLINEABLE readEither #-} {- | Generalized version of 'Prelude.show'. Unlike 'Prelude.show' this function