Help with builder pattern

Hi, I’m looking for help constructing a function. I’m trying to build a wrapper on top of the ReactNative.Style bindings (Getting Started | ReScript React Native) that makes them a bit more concise to use by passing in Varients and using them to construct the style object.

A rough sketch of what I’m looking for is below:

type output = {
  a: option<int>,
  b: option<int>,
  c: option<int>,

@obj external foo: (~a: int=?, ~b: int=?, ~c: int=?, unit) => output = ""

type builder = WithA(int) | WithB(int) | WithC(int)

type build = array<builder> => output

/* Desired Usage:
let x = build([WithA(1), WithC(4)]) // x = {a:1, c:4}
let y = build([WithB(2), WithC(4)]) // x = {b:2, c:4}

I can’t figure out how to write the build function, any ideas?

Hello, i use the builder pattern in 2 ways in my code:

The first one (decalration):

module Crypto = {
  open Models
  type t = {
    ticker: option<Ticker.t>,
    name: option<Name.t>,

  let aCrypto = {
    ticker: None,
    name: None,

  let withTicker = (builder, ticker) => {...builder, ticker: Some(ticker)}
  let withName = (builder, name) => {...builder, name: Some(name)}
  let build: t => Crypto.t = builder => {
    ticker: builder.ticker->Option.getWithDefault(Ticker.make("None")),

The usage:

let btc = aCrypto->withTicker(Ticker.make("BTC"))->withName(Name.make("Bitcoin"))->build

For the second way of doing it, i decided to use a function with optional parameter (Declaration):

let buildContainer = (
  ~findReadModel=_ => of_([Error("storage not initialized")]),
  ~saveReadModel=_ => of_([Error("storage not initialized")]),
) => {
  findReadModel: findReadModel,
  saveReadModel: saveReadModel,

And the usage:


Don’t hesitate if you have any questions :slightly_smiling_face: