相关疑难解决方法(0)

为什么我不能使用具有存在量化类型的记录选择器?

使用存在类型时,我们必须使用模式匹配语法来提取foralled值.我们不能将普通记录选择器用作函数.GHC报告错误并建议使用与以下定义的模式匹配yALL:

{-# LANGUAGE ExistentialQuantification #-}

data ALL = forall a. Show a => ALL { theA :: a }
-- data ok

xALL :: ALL -> String
xALL (ALL a) = show a
-- pattern matching ok

-- ABOVE: heaven
-- BELOW: hell

yALL :: ALL -> String
yALL all = show $ theA all
-- record selector failed
Run Code Online (Sandbox Code Playgroud)
forall.hs:11:19:
    Cannot use record selector `theA' as a function due to escaped type variables
    Probable fix: use …
Run Code Online (Sandbox Code Playgroud)

haskell record existential-type

28
推荐指数
2
解决办法
2392
查看次数

标签 统计

existential-type ×1

haskell ×1

record ×1