我有这个文件:
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ExistentialQuantification #-}
module Toy where
import Control.Lens
data Bar = Bar { _barish :: String }
data Foo = forall a. Show a => Foo { _fooish :: a }
$(makeLenses ''Bar)
$(makeLenses ''Foo)
x = barish
y = fooish
我收到以下错误消息:
Toy.hs:15:5:
Not in scope: `fooish'
Perhaps you meant `_fooish' (line 9)
这是我第一次尝试使用存在量词;我不知道为什么这种功能组合会失败。更令人担忧的是,为什么我没有收到有关 makeLenses 失败的错误消息?我运行了 runhaskell Toy.hs
最佳答案
你实际上不能使用你的函数_fooish。如果您尝试这样做,您会收到错误:
Cannot use record selector `_fooish' as a function due to escaped type variables
Probable fix: use pattern-matching syntax instead
In the expression: _fooish
所以lens无法为你生成镜头。为什么它不给出错误?好吧,有时您有可以为其生成镜头的其他字段。看来这里的情况并非如此,但我认为一般来说 makeLenses 只是跳过所有不可能做的事情并尝试生成其余的事情。
关于haskell - 存在量词默默地破坏了 Template Haskell (makeLenses)。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17256091/