首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >纯透镜合成型误差

纯透镜合成型误差
EN

Stack Overflow用户
提问于 2015-01-12 16:15:00
回答 1查看 153关注 0票数 1

我试图使用纯文本镜头来更新嵌套记录的属性。但是,当我合成镜头以获得属性时,会出现以下类型错误:

代码语言:javascript
运行
复制
Warning: Error at src/Main.purs line 150, column 38 - line 152, column 3: 
Error in declaration performAction
Cannot unify { description :: u24500 | u24501 } with Main.Item. Use --force to continue.

我对镜片和纯文本比较陌生,所以这可能是简单而明显的事情。

产生此错误的相关代码如下(是的,它基于纯温度-todomvc):

代码语言:javascript
运行
复制
data Action
  = NewItem String String String String String
  | RemoveItem Index
  | SetEditText String
  | DoNothing

data Item = Item
            { description :: String
            , keywords :: String
            , link_title :: String
            , profile :: String
            , title :: String
            }

data State = State
  { item :: Item
  , editText :: String
  }

_State :: LensP State { item :: _, editText :: _ }
_State f (State st) = State <$> f st

item :: forall r. LensP { item :: _ | r } _
item f st = f st.item <#> \i -> st { item = i }

editText :: forall r. LensP { editText :: _ | r } _
editText f st = f st.editText <#> \i -> st { editText = i }

itemDescription :: forall r. LensP { description :: _ | r } _
itemDescription f it = f it.description <#> \d -> it { description = d }

performAction :: T.PerformAction _ Action (T.Action _ State)
performAction _ action = T.modifyState (updateState action)
  where
  updateState :: Action -> State -> State
  updateState (NewItem s1 _ _ _ _) = _State .. item .. itemDescription .~ s1
  updateState (SetEditText s)    = _State .. editText .~ s
  updateState DoNothing          = id

我试图更新的属性是st.item.description,上面的错误引用了启动"updateState (NewItem.奇怪的是,下一行也报告了相同的错误。

对如何解决类型错误有什么想法吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-13 12:12:53

我已经“修正”了这一点,使镜片的类型不那么一般。我也把镜头建立在菲尔在他的"24天“纯正镜头回顾中使用的语法上。我觉得语法不那么不透明。

代码语言:javascript
运行
复制
item :: LensP State Item
item = lens (\(State st) -> st.item) (\(State st) item -> State (st { item = item }))

editText :: LensP State String
editText = lens (\(State st) -> st.editText) (\(State st) editText -> State (st { editText = editText }))

itemDescription :: LensP Item String
itemDescription = lens (\(Item it) -> it.description) (\(Item it) description -> Item (it { description = description }))

同样,为了保持镜头类型的简单性,我取消了_State镜头在performAction中的使用

代码语言:javascript
运行
复制
performAction :: T.PerformAction _ Action (T.Action _ State)
performAction _ action = T.modifyState (updateState action)
  where
  updateState :: Action -> State -> State
  updateState (NewItem s1 _ _ _ _) = \st -> st # item..itemDescription.~ s1
  updateState (SetEditText s)    = \st -> st # editText.~ s
  updateState DoNothing          = id

我相信有一个更优雅,全面和完整的解决方案,但这将不得不等到我更好地理解纯镜头。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27906291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档