Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement _Coerce :: TomlBiMap #289

Closed
chshersh opened this issue May 16, 2020 · 0 comments · Fixed by #299
Closed

Implement _Coerce :: TomlBiMap #289

chshersh opened this issue May 16, 2020 · 0 comments · Fixed by #299
Assignees
Labels
codec Conversion between TOML and custom user data types

Comments

@chshersh
Copy link
Contributor

I noticed that it's sometimes useful to be able to create TomlBiMap for newtype if you already have a TomlBiMap for the type itself. For example:

newtype Portion = Portion
    { unPortion :: Double
    }

_Portion :: TomlBiMap Portion AnyValue
_Portion = _Coerce _Double

Currently, it's not straightforward to create custom TomlBiMaps for newtypes. So I think we can implement _Coerce to help with this. And, as a proof of concept, we can implement _BatmanDouble BiMap using _Coerce

-- | Wrapper over 'Double' and 'Float' to be equal on @NaN@ values.
newtype Batman a = Batman
{ unBatman :: a
} deriving stock (Show)

and get rid of testDouble for BiMap:

-- Double needs a special test because NaN /= NaN
testDouble :: PropertyT IO ()
testDouble = hedgehog $ do
x <- forAll G.genDouble
if isNaN x
then assert $
fmap isNaN (forward B._Double x >>= backward B._Double) == Right True
else (forward B._Double x >>= backward B._Double) === Right x

@chshersh chshersh added the codec Conversion between TOML and custom user data types label May 16, 2020
@chshersh chshersh added this to the v1.3.0.0: Big update milestone May 16, 2020
@vrom911 vrom911 self-assigned this May 17, 2020
vrom911 added a commit that referenced this issue May 17, 2020
chshersh pushed a commit that referenced this issue May 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codec Conversion between TOML and custom user data types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants