Skip to content

Proposal to provide [@bs.objFn] #4312

Closed
@Risto-Stevcev

Description

@Risto-Stevcev

This proposal is to provide a [@bs.objFn]. This would work similar to [@@bs.obj] function
https://bucklescript.github.io/docs/en/object-2#function

The benefits of bs.obj over other techniques is that:

  1. You can have an object with many optional values and only provide the entries you need to the function, rather than type None many times like you would have to do with records of Js.t
  2. The transpiled code is clean and not verbose, it's a straightforward object which discludes the fields that you never included, which is important for some libraries that rely heavily on this kind of stuff and want to maintain small bundle sizes

The drawback to using it is that it's not ergonomic. It works okish for configs, but not that great for the JS FFI or for writing a library that heavily relies on optional values while trying to maintain a small bundle size. This proposal is for syntax like this:

external someFn: string -> (?foo: string -> ?bar: int -> ?baz -> bool -> unit [@bs.objFn]) -> t = "someFn" [@@bs.module "some-module"]
let x = someFn "hi" ~bar:123 ~baz:true () 

which compiles to:

var x = someFn("hi", { bar: 123, baz: true})

To do something similar with [@@bs.obj] would require something like someFn "hi" (someFnObj ~bar:123 ~baz:true ()) which is very verbose

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions