I am trying to pattern-match Text to aeson Value using the String constructor, and am running into compile errors. The following example program illustrates the issue I've been running into.
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text
import qualified Data.Aeson
main = print $
case ("here" :: Data.Text.Text) of
(Data.Aeson.String x) -> "match"
_ -> "no match"
As shown I am trying to get the Text "here" to pattern-match into Aeson's 'String' Value constructor. I think that this program should pattern match the Text to Aeson String and print "match". But the program does not compile. Instead I get the following error:
$ ghc scratch.hs
[1 of 1] Compiling Main ( scratch.hs, scratch.o )
scratch.hs:9:4: error:
• Couldn't match expected type ‘Data.Text.Text’
with actual type ‘Data.Aeson.Value’
• In the pattern: Data.Aeson.String x
In a case alternative: (Data.Aeson.String x) -> "match"
In the second argument of ‘($)’, namely
‘case ("here" :: Data.Text.Text) of {
(Data.Aeson.String x) -> "match"
_ -> "no match" }’
It says the pattern expected Text whereas the actual type was aeson Value. But it is clearly Text as marked by the type annotation ("here" :: Data.Text.Text). I don't understand why I am getting this error. Pattern-matching Text to aeson Value via the String constructor appears to be done in this aeson tutorial, and I don't see why it does not also work in my example here.