Skip to content

Add a FromParam instance for String. #16

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions fn/src/Web/Fn.hs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ instance FromParam Text where
fromParam [x] = Right x
fromParam [] = Left ParamMissing
fromParam _ = Left ParamTooMany
instance {-# INCOHERENT #-} FromParam String where
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell me a little more about why "INCOHERENT" is needed here? I've never seen it before.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. The reason is that String is actually [Char] and so GHC complains that it is overlapped with the FromParam [a] instance below. (According to the doc, GHC will not take context (i.e. constrains before =>) into account when matching instances.)
One solution is to add "INCOHERENT" pragma according to this. I am not sure if there is a better solution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Let me think a little about whether this feature is worth that extra bit of type complexity. Regardless I appreciate you taking the time to make this PR

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. Thank you very much!

fromParam [t] = Right (T.unpack t)
fromParam [] = Left ParamMissing
fromParam _ = Left ParamTooMany
instance FromParam Int where
fromParam [t] = case decimal t of
Left _ -> Left ParamUnparsable
Expand Down
2 changes: 2 additions & 0 deletions fn/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ main = hspec $ do
describe "parameter parsing" $
do it "should parse Text" $
fromParam ["hello"] `shouldBe` Right ("hello" :: Text)
it "should parse String" $
fromParam ["hello"] `shouldBe` Right ("hello" :: String)
it "should parse Int" $
do fromParam ["1"] `shouldBe` Right (1 :: Int)
fromParam ["2011"] `shouldBe` Right (2011 :: Int)
Expand Down