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

Cannot able create a Record from another Record (With all optional keys)

$
0
0

It’s not pretty, but the boilerplate can be written in rescript, via the object spread operator. Working off of the initial example:

type a = {
  t ?: string,
  l ?: int,
  p ?: array<int>
}

type b = {
  t ?: string,
  l ?: string,
  p ?: string
}

let updateT : (b, a) => b = (b, a) => {
	switch a.t {
  	 | Some(r) => {...b, t: r}
     | None => b
  }
}
let updateL : (b, a) => b = (b, a) => {
	switch a.l {
  	 | Some(r) => {...b, l: "int"}
     | None => b 
  }
}
let updateP : (b, a) => b = (b, a) => {
	switch a.p {
  	 | Some(r) => {...b, p: "array"}
     | None => b
  }
}

let mapper: (a) => b = (a) => 
  {}
  ->updateT(a)
  ->updateL(a)
  ->updateP(a)

Viewing all articles
Browse latest Browse all 2592

Trending Articles