Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 1784

Allow using `...` operator to shallow copy a record without rewrite any fields

$
0
0
type state = {
  id: string,
  frames: array<Js.TypedArray2.Uint8Array.t>,
  duration: float,
  width: int,
  height: int,
}

type mutableState = {
  mutable id: string,
  mutable frames: array<Js.TypedArray2.Uint8Array.t>,
  mutable duration: float,
  mutable width: int,
  mutable height: int,
}

let state: state = {
  id: "1",
  frames: [Js.TypedArray2.Uint8Array.fromLength(100)],
  duration: 10.,
  width: 100,
  height: 100
}

let mutableState: mutableState = {
  ...(state :> mutableState),
  id: "2" // must has a field
}

let mutableState: mutableState = {
  ...(state :> mutableState),
}

playground here

Currently, just only use ... to shallow copy a record is not allowed, you must rewrite some fields at the end of the new record. But the need for shallow copying a record by using ... in ts/js is very normal and useful, i think we can add this into Rescript!


Viewing all articles
Browse latest Browse all 1784

Trending Articles