haskell - 存在量词默默地破坏了 Template Haskell (makeLenses)。为什么?

标签 haskell template-haskell lenses

我有这个文件:

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

相关文章:

haskell - Haskell 中的状态机模式 : infinite type error

haskell - 使用 monadic rank-2 类型

haskell - 如何动态调用在同一签名中的多个模块中定义的函数

haskell - 为什么 Template Haskell 在编译期间允许任意 IO 操作?

scala - 如何使用 Circe Optics 解析嵌套的 Json 数组

haskell - 汇总 Haskell 记录列表

haskell - 是否存在渐近优化一系列 MonadPlus 操作的 Codensity MonadPlus?

haskell - 罗马人、 ruby 和 haskell

javascript - lambda js : lens for deeply nested objects with nested arrays of objects

haskell - 您可以使用 IO 函数(在 do 语句中)设置记录的字段吗?