2015-01-12 45 views
1

我想用purescript-lens更新嵌套記錄的屬性。然而,當我撰寫的鏡頭獲得的財產,我得到以下類型的錯誤:purescript-鏡頭組成類型錯誤

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 - 鋁熱-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

我已經「固定」了這一點,使鏡頭的類型不那麼一般。我還基於菲爾在他的"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。