本文介绍了镜片和类型家庭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了将Control.Lens
一起使用的问题-XTypeFamilies GHC编译指示时使用数据类型.

I've encountered a problem of using Control.Lens together with
datatypes while using the -XTypeFamilies GHC pragma.

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies    #-}

import Control.Lens (makeLenses)

class SomeClass t where
  data SomeData t :: * -> *

data MyData = MyData Int

instance SomeClass MyData where
  data SomeData MyData a = SomeData {_a :: a, _b :: a}

makeLenses ''SomeData

错误消息是:reifyDatatype: Use a value constructor to reify a data family instance.

有什么办法可以克服它,也许使用Control.Lens中的某些功能?

Is there any way to overcome it, maybe using some functional from Control.Lens?

推荐答案

最明智的做法是自行定义这些镜头……这并不困难:

The most sensible thing would be to just define those lenses yourself... it's not like it's very difficult:

a, b :: Lens' (SomeData MyData a) a
a = lens _a (\s a' -> s{_a=a'})
b = lens _b (\s b' -> s{_b=b'})

甚至

a, b :: Functor f => (a -> f a) -> SomeData MyData a -> f (SomeData MyData a)
a f (SomeData a₀ b₀) = (`SomeData`b₀) <$> f a₀
b f (SomeData a₀ b₀) =   SomeData a₀  <$> f b₀

...它根本不使用镜头库中的任何内容,但与所有镜头组合器完全兼容.

...which doesn't use anything from the lens library at all, but is fully compatible to all lens combinators.

这篇关于镜片和类型家庭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 05:20