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

instance {From,To}Field Bool #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fisx
Copy link

@fisx fisx commented Dec 31, 2015

I noticed that Bool doesn't have field instances, and propose to add these.

This PR contains all I need for now, but I'll ramble on a little about alternatives just in case. Feel free to skip to the code and ignore the rest of this text. (-:

I have also thought about making the FromField instance more lenient (accepting 'yes', '1', ...), but didn't like the assymetry and the expected performance penalty. We could give the choice to the library user, though (not type-checked):

import Data.CaseInsensitive

newtype LenientBool = LenientBool { fromLenientBool :: Bool }

instance FromField LenientBool where
    parseField s = pure $ mk s `elem` ["true", "yes", "on", "1"]

instance ToField LenientBool where
    toField (LenientBool True)  = "True"
    toField (LenientBool False) = "False"

Choices:

  • do not depend on CaseInsensitive and just make the list longer? (what about trUE?)
  • make LenientBool less lenient and still reject Essex St. (also reject 300, or read it as True? what about 300.2?)
  • write true, false in order to make the two types distinguishable in the output?

Such a small, simple problem, and so many things to consider! :-)

@tfausak
Copy link

tfausak commented Mar 27, 2017

I think this would be nice to have. As #119 notes, there's no standard way to encode booleans in CSVs. Case in point, I'm parsing a file now that uses true and false. I have to use a newtype for that, but it would be nice to be able to encode booleans by default.

@hvr
Copy link
Collaborator

hvr commented Jun 15, 2017

I know that the Python csv-writer happens to encode as the strings True/False as well; I'd be curious to know what other mainstream CSV libraries and programs use as the convention to encode booleans before considering attaching a canonical instance to Bool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants