我定义了一个可扩展的记录
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
函数?
这就是它的完成方式。 (可选)您可以模式匹配参数:
Maybe.map
(\{x, y} ->
{ model
| x = x
, y = y
}
)