-1

Pretty much all bucklescript examples have something that has a syntax like this:

[@bs.send.pipe : t('options)] external parse : array(string) => 'options = "parse";

or like this:

[@bs.module "express"] external make : (string, options) => t = "static";

The simplest example I've seen is in this tutorial:https://medium.com/@Hehk/binding-a-library-in-reasonml-e33b6a58b1b3

type t;

[@bs.module] external commander : t = "";

What is the last = string representing? And what is type t in this instance?

glennsl
  • 28,186
  • 12
  • 57
  • 75
Abraham P
  • 15,029
  • 13
  • 58
  • 126

2 Answers2

2

t is a type without a definition, which is called an abstract type. Usually it's used in an interface to hide the implementation, so that you can't access and manipulate it without using specific associated functions to do so, but it doesn't actually require any implementation at all. This is particularly useful with foreign language interop, since the implementation is in a different language.

The string after = is the name of the "foreign" function, so the JavaScript identifier in this case. The make external will therefore produce something like require('express').static(...) instead of require('express').make(...).

The parse external uses the same name in both OCaml and JavaScript, in which case the foreign name can be omitted, which is what commander does. By leaving it an empty string it will get inferred from the name of the external.

glennsl
  • 28,186
  • 12
  • 57
  • 75
0

The last = string depends on the context, which is set by the [@bs.blabla] that prepends the external definition.

For example, in this snippet:

[@bs.module "express"] external make : (string, options) => t = "static";

We're creating a make function from static method of the the "express" module because we're using [@bs.module "express"].

More info here: https://bucklescript.github.io/docs/en/intro-to-external.html

John Paul Ada
  • 537
  • 5
  • 9