我定义了一个可扩展的记录
type alias Saved a =
{ a
| x : Int
, y : String
}
和基于此的Model
:
type alias Model =
Saved { z : Float }
然后我将JSON加载并解码为Saved {}
:
let
received =
Decode.decodeValue savedDecoder json |> Result.toMaybe
in
(Maybe.map
(\r ->
{ model
| x = r.x
, y = r.y
}
)
received
|> Maybe.withDefault model
是否有任何方法可以将现有的model
与received
可扩展记录合并,而无需像ES6 Object.assign
函数那样单独复制每个字段?
答案 0 :(得分:2)
就是这样。 (可选)您可以对参数进行模式匹配:
Maybe.map
(\{x, y} ->
{ model
| x = x
, y = y
}
)