且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

存在量词默默地破坏了模板Haskell(makeLenses)。为什么?

更新时间:2023-11-19 23:43:58

你的功能_fooish。如果你试图这样做,你会得到这样的错误:

$ $ $ pre $ $ code>由于转义类型,不能使用记录选择器_fooish作为函数变量
可能的修正:使用模式匹配语法来代替
在表达式中:_fooish




I have this file:

{-# 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

and I get the following error message:

Toy.hs:15:5:
    Not in scope: `fooish'
    Perhaps you meant `_fooish' (line 9)

This is my first time attempting to use existential quantifiers; I have no idea why this combination of features breaks. Even more worryingly, why do I get no error message about makeLenses failing? I ran runhaskell Toy.hs

You can't actually use your function _fooish. If you try to do that, you get the error:

Cannot use record selector `_fooish' as a function due to escaped type variables
Probable fix: use pattern-matching syntax instead
In the expression: _fooish

So lens can't generate a lens for you. Why doesn't it give an error? Well, sometimes you have additional fields for which it's possible to generate lenses. It seems this not the case here, but I think in general makeLenses just skips everything that is impossible to do and tries to generate the rest.