Why does `showTextData` convert everything to lower case
I was hoping readTextData and showTextData would form a nice bijection, so that I could easily derive the ability to send my custom types over the wire without too much thought, but for some reason showTextData converts everything to lower case, causing readTextData to fail.
What is the reasoning behind this? IMO the naming is a little misleading and error prone.
I was hoping
readTextDataandshowTextDatawould form a nice bijection
There cannot be a bijection (because not every Text can be parsed in general), but you probably meant this property
readTextData . showTextData == Right -- does not hold: showTextData does lowercasing
What is the reasoning behind this?
I can't recall it now, but think the reasoning was that showt (or Text.pack . show) does the regular show-like serialisation already, and I needed a lowercase one for case-insensitive helpers, so I made showTextData to be that lowercase helper. Could've named it differently, I guess 😅
IMO the naming is a little misleading and error prone.
You're right, naming is a bit misleading (although I have tried to make documentation clear 😄 ).
Functions that do have the property you are looking for are
-
showt(fromtext-show) and -
Text.pack . show.
readTextData . showt == Right
readTextData . Text.pack . show == Right
Examples:
>>> quickCheck (\x -> readTextData (showt (x :: String)) == Right x)
+++ OK, passed 100 tests.
data Person = Person { fullName :: String, age :: Int } deriving (Eq, Show, Read)
instance Arbitrary Person where arbitrary = Person <$> arbitrary <*> arbitrary
>>> quickCheck (\x -> readTextData (Text.pack (show (x :: Person))) == Right x)
+++ OK, passed 100 tests.
You are right that it's not a bijection, faithful serialization I guess is what i'm after. I ended up reading the docs after the fact and discovered my mistake, but initially I just noticed the existence of showTextData and made some assumptions.
pack . show ended up doing the job. Although it's worth noting that the existence of a showTextData function that did lowercase and no function that didn't, gave me the impression that there was something wrong with uppercase letters in URIs, so one purely doc change that might be worthwhile is just highlight that if you don't want lowercasing use pack . show.
so one purely doc change that might be worthwhile is just highlight that if you don't want lowercasing use pack . show.
Would you do the honours? 😊