我有一些記錄SomeRecord和欄位,例如_user, _password。我想Getter為一個“虛擬”欄位寫一個,比如身份,它看起來像<user>:<password>:
identity:: Getter SomeRecord String
identity = to $ \srec -> srec^.user <> ":" <> srec^.password
這個吸氣劑會導致警告:
? Redundant constraint: Functor f
? In the type signature for:
identity :: Getter SomeRecord Stringtypecheck(-Wredundant-constraints)
這個吸氣劑有什么問題以及如何在沒有警告的情況下撰寫它?
編輯:
剛剛在網上找到了這個帖子:
評論(由ekmett):
為了不被
lensGHC 8 用戶的抱怨淹沒,我們可能會在實踐中添加約束to并依靠{-# INLINE #-}洗掉它。這是一個常見的場景,足以保證我們吃掉我們身邊的噪音。當然,如果您將不遵守法律的遍歷宣告為折疊等,您會得到同樣的結果。恐怕唯一的答案就是壓制警告。GHC 看不到“法律”。
uj5u.com熱心網友回復:
這是由于 Edward Kmett 的鏡頭庫的作業方式。
Getter是型別同義詞:
type Getter s a = forall f. (Contravariant f, Functor f) => (a -> f a) -> s -> f s
同時型別to為:
to :: (Profunctor p, Contravariant f) => (s -> a) -> Optic' p f s a
沒關系Optic',問題是Getter有約束Functor f但to不需要它。因此發出警告,因為否則您可以將它與f不是仿函式的型別一起使用。
您的函式的“正確”型別類似于:
identity :: (Profunctor p, Contravariant f) => Optic' p f SomeRecord String
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/530208.html
標籤:哈斯克尔哈斯克尔镜头
上一篇:替換串列的多個元素
