Skip to content

Commit

Permalink
[#286] Consistent type for 'readMaybe' and 'readEither' (#296)
Browse files Browse the repository at this point in the history
* [#286] Consistent type for 'readMaybe' and 'readEither'

Resolves #286

* Update CHANGELOG.md

Co-authored-by: Dmitrii Kovanikov <[email protected]>

Co-authored-by: Dmitrii Kovanikov <[email protected]>
  • Loading branch information
vrom911 and chshersh authored May 14, 2020
1 parent 80ffb14 commit 7eb0ce0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/Relude/String.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
-}
11 changes: 6 additions & 5 deletions src/Relude/String/Conversion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7eb0ce0

Please sign in to comment.