Skip to content

More uniform bs.module/scope external #2067

Closed
@chenglou

Description

@chenglou

One recurring question we get is how to bind to a module itself, how to bind to a default, how to bind to a nested value and how to bind to a global one. I'm proposing that we unify these through:

(* var X = require('x'); X.add *)
external add : int -> int -> int = "" [@@bs.require ("x", "add")]
(* bind to the module itself *)
external fs : someType = "" [@@bs.require "fs"]
(* window.location *)
external location : someType = "" [@@bs.require ("window", "location")][@@bs.global]
(* bind to document *)
external dom : dom = "" [@@bs.require "document"][@@bs.global]

Characteristics:

  • It'll always be = "". This makes things much more consistent for newcomers. It's a recurring question of where to put what. Maybe we can choose another format now thanks to this.
  • People also ask whether to use [@@bs.val "Math.imul"] or the bs.scope equivalent, etc.
  • Composes with bs.new and the rest as expected.
  • Unifies bs.module, bs.scope, bs.val under a similar looking API. No bs.module module renaming facility, but that's rather rare. These are still kept for backward compat.
  • Resolves the question "what if I want to bind to a module that's global?" through bs.global, which is easier to remember.
  • Might have some nice opportunities to work with bs.get/set.

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