在 Elm 0.19 中合并具有可扩展记录的模型
Merge model with extensible record in Elm 0.19
我定义了一个可扩展记录
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
}
)
我定义了一个可扩展记录
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
}
)