BuckleScript

BuckleScript

  • Docs
  • Try
  • API
  • Community
  • Blog
  • Languages iconEnglish
    • 日本語
    • Español
    • Français
    • 한국어
    • Português (Brasil)
    • Русский
    • 中文
    • 繁體中文
    • Help Translate
  • GitHub

›Interop

Intro

  • What & Why
  • Installation
  • New Project
  • Try
  • Concepts Overview
  • Upgrade Guide to v7

Interop

  • Overview
  • Cheatsheet
  • Embed Raw JavaScript
  • Common Data Types
  • Intro to External
  • Bind to Global Values
  • Null, Undefined & Option
  • Object
  • Object 2
  • Class
  • Function
  • Property access
  • Return value wrapping
  • Import & Export
  • Regular Expression
  • Exceptions
  • JSON
  • Pipe First
  • Generate Converters & Helpers
  • Better Data Structures Printing (Debug Mode)
  • NodeJS Special Variables
  • Miscellaneous
  • Browser Support & Polyfills

Build System

  • Overview
  • Configuration
  • Automatic Interface Generation
  • Interop with Other Build System
  • Performance
  • Advanced

Standard Library

  • Overview

Advanced

  • Conditional Compilation
  • Extended Compiler Options
  • Use Existing OCaml Libraries
  • Difference from Native OCaml
  • Compiler Architecture & Principles
  • Comparison to Js_of_ocaml
Edit

Intro to External

Many parts of the interop system uses a concept called external, so we'll specially introduce it here.

external is a keyword for declaring a value in BuckleScript/OCaml/Reason:

external myCFunction : int -> string = "theCFunctionName"
external myCFunction : int => string = "theCFunctionName";

It's like a let, except that the body of an external is, as seen above, a string. That string usually has specific meanings depending on the context. For native OCaml, it usually points to a C function of that name. For BuckleScript, these externals are usually decorated with certain [@bs.blabla] attributes.

Once declared, you can use an external as a normal value.

BuckleScript externals are turned into the expected JS values, inlined into their callers during compilation, and completely erased afterward. You won't see them in the JS output. It's as if you wrote the generated JS code by hand! No performance cost either, naturally.

Note: do also use externals and the [@bs.blabla] attributes in the interface files. Otherwise the inlining won't happen.

Special Identity External

One external worth mentioning is the following one:

external myShadyConversion : foo -> bar = "%identity"
external myShadyConversion : foo => bar = "%identity";

This is a final escape hatch which does nothing but convert from the type foo to bar. In the following sections, if you ever fail to write an external, you can fall back to using this one. But try not to.

Last updated on 4/13/2020
← Common Data TypesBind to Global Values →
  • Special Identity External