Paul Krill
Editor at Large

OCaml compiler brings type safety to JavaScript

news
Sep 7, 20162 mins

BuckleScript from Bloomberg takes OCaml code and turns it into JavaScript

Web developers can use the OCaml language to write JavaScript code through the BuckleScript compiler, which has now gone to a 1.0 release. Open-sourced by news services provider Bloomberg, BuckleScript takes OCaml code and turns out JavaScript.

OCaml is a general-purpose language that supports imperative, functional, and object-oriented programming. “OCaml offers an industrial-strength state-of-the-art type system and provides very strong type inference (i.e. unlike TypeScript, no verbose type annotation is required), which proves invaluable in managing large projects,” Bloomberg’s Hongbo Zhang said.

Internal benchmarking has BuckleScript compiling at least 10 times faster than TypeScript, Microsoft’s superset that compiles to plain JavaScript. “There is no magic here, the BuckleScript compiler is written in OCaml, which is compiled to blazing fast native code,” said Zhang. “However, it does not limit the availability of [the] BuckleScript compiler: it is also compiled to a JS file (gzipped: 700KB for pure OCaml + 300KB for Reason support).”

BuckleScript is intended to address large-scale problems with JavaScript, according to the compiler’s user manual. These include type safety, elimination of dead code, and offline optimizations. BuckleScript uses the OCaml type system and compiler implementation to optimize offline compilation and provide for fast runtime code. Loss of code structure is addressed by mapping one OCaml module onto one JavaScript module.

BuckleScript utilizes the OCaml compiler and toolchain to produce JavaScript closely resembling original Ocaml code, according to the project’s GitHub page. Name mangling is avoided, while stack traces are preserved. Programs can be compiled into native code, JavaScript, or a unikernel. The foreign function interface (FFI) in BuckleScript can model JavaScript objects natively. Some OCaml features highly coupled with runtimes, such as Gc and Unix modules, are not covered in BuckleScript.

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorld’s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorld’s audience of software developers and other information technology professionals. Paul has won a “Best Technology News Coverage” award from IDG.

More from this author