在Elm 0.19中合并具有可扩展记录的模型

问题描述 投票:1回答:1

我定义了一个可扩展的记录

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

是否有任何方法可以将现有的modelreceived可扩展记录合并,该记录不涉及单独复制每个字段,类似于ES6 Object.assign函数?

merge record elm
1个回答
2
投票

这就是它的完成方式。 (可选)您可以模式匹配参数:

Maybe.map
  (\{x, y} ->
    { model
    | x = x
    , y = y
    }
  )

© www.soinside.com 2019 - 2024. All rights reserved.