Skip to content

Latest commit

 

History

History
109 lines (69 loc) · 1.76 KB

bs.send.md

File metadata and controls

109 lines (69 loc) · 1.76 KB

[@bs.send]

[@bs.send]

bs.send is used to call a function on an object.

For example, say you created a div and a button:

type element;
[@bs.scope "document"] [@bs.val] external createElement: string => element = "";

let div = createElement("div");
let button = createElement("button");

How can we append the button to the div?

Doing this doesn't work:

div.appendChild(button); // Error: The record field appendChild can't be found.

That's what bs.send is for:

[@bs.send] external appendChild: (element, element) => element = "";

appendChild(div, button);

which compiles to:

div.appendChild(button);

In general, bs.send external looks like this:

[@bs.send] external fn: (obj, arg1, arg2, arg3, ... , argN) => ...;

You call fn like this:

fn(obj, arg1, arg2, arg3, ... , argN);

/*
  or equivalently:

  obj->fn(arg1, arg2, arg3, ... , argN);
*/

and BS compiles this to:

obj.fn(arg1, arg2, arg3, ... , argN);

What if you want the object to come after the arguments like so:

fn(arg1, arg2, arg3, ... , argN, obj);

/*
  or equivalently:

  obj |> fn(arg1, arg2, arg3, ... , argN);
*/

That's what bs.send.pipe is for.

[@bs.send.pipe]

bs.send.pipe, like bs.send, is used to call a function on an object.

Unlike bs.send, bs.send.pipe takes the object as an argument:

[@bs.send.pipe: obj] external fn: (arg1, arg2, arg3, ... , argN) => ...;

You call fn like this:

fn(arg1, arg2, arg3, ... , argN, obj);

/*
  or equivalently:

  obj |> fn(arg1, arg2, arg3, ... , argN);
*/

and BS compiles this to:

obj.fn(arg1, arg2, arg3, ... , argN);