Existential quantifier silently disrupts Template Haskell (makeLenses). Why?

限于喜欢 提交于 2019-12-24 03:09:10

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/17256091/existential-quantifier-silently-disrupts-template-haskell-makelenses-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!