我正在尝试使用purescript-lens来更新嵌套记录的属性。但是,当我组装镜头到达房产时,我得到以下类型错误:
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.
我对镜头和purescript相对较新,所以它可能很简单明了。
产生此错误的相关代码如下(是的,它基于purescript-thermite-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 ...”)的行。奇怪的是,下一行也会报告相同的错误。
有关如何解决类型错误的任何想法?
由于
答案 0 :(得分:0)
我已修复&#34;这通过使镜片的类型不那么通用。我还根据Phil在其"24 days" review of purescript-lens中使用的语法建立了镜头。我觉得语法不那么透明。
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
我确信这是一个更优雅,更通用,更完整的解决方案,但这必须等到我更好地了解purescript-lens。