小编jek*_*339的帖子

Ad hoc多态函数

我有一些我想要打印的数据(有些是Maybe,有些则不是),我正在尝试创建一个通用的showField函数,如下所示:

showField :: (Show a) => a -> Text
showField x
  | isJust x = Text.pack $ show $ fromJust x
  | isNothing x = "None"
  | otherwise = Text.pack $ show x
Run Code Online (Sandbox Code Playgroud)

这是一个严格的类型错误:

• Couldn't match expected type ‘Maybe a0’ with actual type ‘a’
  ‘a’ is a rigid type variable bound by
    the type signature for:
      showField :: forall a. Show a => a -> Text
    at /data/users/jkozyra/fbsource/fbcode/experimental/jkozyra/hs/holdout_cleanup/HoldoutReaper.hs:244:18
• In the first argument of ‘isNothing’, namely ‘x’
  In the …
Run Code Online (Sandbox Code Playgroud)

haskell functional-programming

2
推荐指数
1
解决办法
209
查看次数

标签 统计

functional-programming ×1

haskell ×1