使用存在类型时,我们必须使用模式匹配语法来提取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)