This is the latest docs version
Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • Twitter
  • GitHub
  • Forum
Old DocsBS@8.2.0
Interop
  • Overview
  • Better Data Structures Printing (Debug Mode)
  • Miscellaneous
    • Composing bs Attributes
    • Safe External Data Handling
  • Decorators
Advanced
  • Conditional Compilation
  • Extended Compiler Options
  • Compiler Architecture & Principles
  • Comparison to Js_of_ocaml
Docs / Old Docs / InteropMisc
IMPORTANT!
This section is still about ReasonML & BuckleScript. It will be rewritten to ReScript very soon.

Interop Miscellaneous

Composing bs Attributes

As you might have guessed, most bs.* attributes can be used together. Here's an extreme example:

Note that bs.splice was renamed to bs.variadic after version 4.08

RE
[@bs.val] [@bs.scope "global"] [@bs.variadic] external draw : ([@bs.as "dog"] _, array(int)) => unit = "draw"; draw([|1, 2|]);

Output:

JS
global.draw("dog", 1, 2);

Safe External Data Handling

In some cases, the data could either come from JS or BS; it's very hard to give precise type information because of this. For example, for an external promise whose creation could come from the JS API, its failed value caused by Promise.reject could be of any shape.

BuckleScript provides a solution, bs.open, to filter out OCaml structured exception data from the mixed data source. It preserves type safety while allowing you to deal with mixed source. It makes use of OCaml’s extensible variant, so that users can mix values of type exn with JS data.

RE
let handleData = [@bs.open] ( fun | Invalid_argument(_) => 0 | Not_found => 1 | Sys_error(_) => 2 ); /* handleData is 'a => option(int) */

For any input source, as long as it matches the exception pattern (nested pattern match supported), the matched value is returned, otherwise return None.

Better Data Structures Printing (Debug Mode)Decorators

© 2024 The ReScript Project

Software and assets distribution powered by KeyCDN.

About
  • Community
  • ReScript Association
Find us on