当一个去糖化的字段是一个关键字时,Lens如何处理?我记得读过一些特殊的处理方式,但我不记得在哪里读到的或“lensed”访问器的名称会变成什么。
考虑以下内容:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
import Control.Monad.IO.Class (liftIO)
import Data.Maybe
import Data.Aeson
import Data.Aeson.TH
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as LC8
import qualified Data.Text.Lazy.Encoding as TLE
data Typ = Typ {
_fld1 :: BS.ByteString
, _type :: Int
} deriving (Show)
$(deriveJSON tail ''Typ)
$(makeLenses ''Typ)
main = do
print $ typ^.fld1
print $ typ^.getType
where
jsonTyp = "{\"fld1\": \"Test\", \"type\": 1 }"
typ' = decode jsonTyp :: Maybe Typ
typ = fromJust typ'
getType :: Getter Typ Int
getType = to _type
_type
访问器应该被称为什么,我如何避免在这里实现getType
?
我不得不在 school of haskell 上匆忙完成这个问题,因为我在这里没有访问一个合适的开发环境,但我想这可能对其他人有用。如果我能够进入ghci并执行:browse
(如果这样可以得到答案),我会添加一个答案的,但与此同时,有人知道吗?
结论
谢谢大家,我将使用makeLensesWith
以及关键词到替换的映射,根据 Edward 的建议。