Skip to content

instance {From,To}Field Bool#107

Open
fisx wants to merge 1 commit into
haskell-hvr:masterfrom
zerobuzz:boolean-fields
Open

instance {From,To}Field Bool#107
fisx wants to merge 1 commit into
haskell-hvr:masterfrom
zerobuzz:boolean-fields

Conversation

@fisx

@fisx fisx commented Dec 31, 2015

Copy link
Copy Markdown

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

tfausak commented Mar 27, 2017

Copy link
Copy Markdown

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

hvr commented Jun 15, 2017

Copy link
Copy Markdown
Collaborator

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