BuckleScript

BuckleScript

  • 資料
  • 試す
  • API
  • コミュニティ
  • ブログ
  • Languages icon日本語
    • English
    • Español
    • Français
    • 한국어
    • Português (Brasil)
    • Русский
    • 中文
    • 繁體中文
    • 翻訳に参加する
  • GitHub

›相互運用

はじめに

  • はじめに
  • インストール
  • プロジェクトの作成
  • Try
  • コンセプト

相互運用

  • 概要
  • チートシート
  • 生のJavaScriptの埋め込み
  • 基本的なデータ型
  • Externalの紹介
  • グローバル変数の束縛
  • Null・UndefinedとOption
  • オブジェクト
  • オブジェクト その2
  • クラス
  • 関数
  • Property access
  • Return value wrapping
  • ImportとExport
  • 正規表現
  • 例外
  • JSON
  • Pipe First
  • 変換と補助の生成
  • より良いデータ構造の出力 (デバッグモード)
  • NodeJSの特殊な変数
  • その他
  • ブラウザのサポートとポリフィル

ビルドシステム

  • 概要
  • 設定
  • インターフェースの自動生成
  • Interop with Other Build System
  • パフォーマンス
  • 上級者向け

標準ライブラリ

  • 概要

上級者向け

  • 条件付きコンパイル
  • コンパイラ拡張の為のオプション
  • 既存のOCamlのライブラリの使用
  • ネイティブのOCamlとの違い
  • コンパイラの設計と原則
  • Js_of_ocamlとの比較
Translate

例外

In the JS world, exception could be any data, while an OCaml exception is a structured data format and supports pattern matching. Catching an OCaml exception on JS side therefore doesn't work as intended.

JS exceptions can be raised from the BuckleScript side by using the JS.Exn.raise* functions, and can be caught as a BS exception of the type Js.Exn.Error with the JS exception as its payload, typed as Js.Exn.t. The JS Exception can then either be manipulated with the accessor functions in Js.Exn, or casted to a more appropriate type.

try
  Js.Exn.raiseError "oops!"
with
| Js.Exn.Error e ->
  match Js.Exn.message e with
  | Some message -> Js.log {j|Error: $message|j}
  | None -> Js.log "An unknown error occurred"
try (
  Js.Exn.raiseError("oops!")
) {
| Js.Exn.Error(e) =>
  switch (Js.Exn.message(e)) {
  | Some(message) => Js.log({j|Error: $message|j})
  | None => Js.log("An unknown error occurred")
  }
};

Usage

Take promise for example:

exception UnhandledPromise

let handlePromiseFailure = function [@bs.open]
  | Not_found ->
    Js.log "Not found";
    Js.Promise.resolve ()

let _ =
 Js.Promise.reject Not_found
 |> Js.Promise.catch (fun error ->
      match handlePromiseFailure error with
      | Some x -> x
      | None -> raise UnhandledPromise
  )
exception UnhandledPromise;

let handlePromiseFailure =
  [@bs.open]
  (
    fun
    | Not_found => {
        Js.log("Not found");
        Js.Promise.resolve()
      }
  );

Js.Promise.reject(Not_found)
  |> Js.Promise.catch(
     (error) =>
       switch (handlePromiseFailure(error)) {
       | Some(x) => x
       | None => raise(UnhandledPromise)
       }
   );
← 正規表現JSON →
  • Usage