最近我在学习 Elm,但是遇到了一个简单的问题,我想要更新记录字段。我的代码如下:
-- MODEL
initialModel : Model
initialModel =
{ selectedLanguage = "german"
, allCards = Card.cards
}
type alias Msg =
{ description : String
, data : String
, id : String
}
更新功能
update : Msg -> Model -> Model
update msg model =
case List.head (model.allCards) of
Just card ->
{ card | fliped = True }
Nothing -> model
但是我看到了这个:
Something is off with the 1st branch of this `case` expression:
50| { card | fliped = True }
^^^^^^^^^^^^^^^^^^^^^^^^
The 1st branch is a record of type:
{ back : String, fliped : Bool, front : String, id : String }
But the type annotation on `update` says it should be:
Model
Hint: Seems like a record field typo. Maybe back should be allCards?
Hint: Can more type annotations be added? Type annotations always help me give
more specific messages, and I think they could help a lot in this case!
Detected errors in 1 module.
我认为根据我的类型定义,应该从update
函数中始终返回一个模型,但是我不知道如何做到。 请问有什么建议吗?
List.map
和一个检查ID并在匹配时返回其他内容的函数,否则什么也不做。例如:List.map (\item -> if item.id == 34 then { item | flipped = False } else item) items
- glennslupdateIf
,它正是这样做的。 - Gelineau