我试图使用纯文本镜头来更新嵌套记录的属性。但是,当我合成镜头以获得属性时,会出现以下类型错误:
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):
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.奇怪的是,下一行也报告了相同的错误。
对如何解决类型错误有什么想法吗?
谢谢
发布于 2015-01-13 12:12:53
我已经“修正”了这一点,使镜片的类型不那么一般。我也把镜头建立在菲尔在他的"24天“纯正镜头回顾中使用的语法上。我觉得语法不那么不透明。
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中的使用
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我相信有一个更优雅,全面和完整的解决方案,但这将不得不等到我更好地理解纯镜头。
https://stackoverflow.com/questions/27906291
复制相似问题