Couldn't Match Expected Type Against Inferred Type, Rigid Type Variable Error

后端 未结 2 465
青春惊慌失措
青春惊慌失措 2020-12-09 15:17

What is wrong with this function ?

test :: Show s => s
test = \"asdasd\"

String is an instance of the Show class, so it see

2条回答
  •  天涯浪人
    2020-12-09 15:36

    Yes, String is an instance of Show. But that doesn't allow using a string as an abritary Show value. 1 can be Num a => a because there's an 1 :: Integer, an 1 :: Double, an 1 :: Word16, etc. If "asdasd" could be of type Show a => a, there would be "asdasd" :: Bool, "asdasd" :: String, "asdasd" :: Int, etc. There isn't. Therfore, "asdasd" can't be of type Show a => a. The type of a string constant doesn't get much more general than String.

提交回复
热议问题