Skip to content

Commit

Permalink
Merge pull request serokell#256 from serokell/dk318/serokell#165-read…
Browse files Browse the repository at this point in the history
…Maybe-type-changing

[serokell#165] `readMaybe` type changing
  • Loading branch information
DK318 authored Apr 26, 2022
2 parents f56a74e + d63872b commit 746fda6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Unreleased
* [#182](https://github.com/serokell/universum/issues/182):
Deprecate `microlens` and `microlens-mtl` dependencies.

* [#165](https://github.com/serokell/universum/issues/165):
Change the type of `readMaybe` from `readMaybe :: Read a => String -> Maybe a`
to it's polymorphic version `readMaybe :: forall b a. (ToString a, Read b) => a -> Maybe b`.

1.7.3
=====

Expand Down
13 changes: 12 additions & 1 deletion src/Universum/String/Conversion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ module Universum.String.Conversion

-- * Show and read functions
, readEither
, readMaybe
, show
) where

import Data.Bifunctor (first)
import Data.Either (Either)
import Data.Function (id, (.))
import Data.Maybe (Maybe)
import Data.String (String)
import qualified Data.Text.Internal as T
import qualified Data.Text.Internal.Fusion.Common as TF
Expand All @@ -47,7 +49,7 @@ import qualified Data.Text.Encoding as T
import qualified Data.Text.Encoding.Error as T
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.Encoding as LT
import qualified Text.Read (readEither)
import qualified Text.Read (readEither, readMaybe)

import qualified GHC.Show as Show (Show (show))

Expand Down Expand Up @@ -293,6 +295,15 @@ be safe to collapse.
readEither :: (ToString a, Read b) => a -> Either Text b
readEither = first toText . Text.Read.readEither . toString

-- | Polymorhpic version of 'Text.Read.readMaybe'.
--
-- >>> readMaybe @Int @Text "123"
-- Just 123
-- >>> readMaybe @Int @Text "aa"
-- Nothing
readMaybe :: forall b a. (ToString a, Read b) => a -> Maybe b
readMaybe = Text.Read.readMaybe . toString

-- | Generalized version of 'Prelude.show'.
show :: forall b a . (Show.Show a, IsString b) => a -> b
show x = fromString (Show.show x)
Expand Down
2 changes: 1 addition & 1 deletion src/Universum/String/Reexport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ import Data.Text.Encoding (decodeUtf8', decodeUtf8With)
import Data.Text.Encoding.Error (OnDecodeError, OnError, UnicodeException, lenientDecode,
strictDecode)
import Data.Text.Lazy (fromStrict, toStrict)
import Text.Read (Read, readMaybe, reads)
import Text.Read (Read, reads)

0 comments on commit 746fda6

Please sign in to comment.