October 31, 2024 â Moonbit builds fast Wasm. It is one of the fastest growing languages in PLDB, cracking the top 300 in less than 2 years. We got the chance to talk to Moonbit's creator, Hongbo Zhang, who explained to us why Moonbit exists and the effort that has gone into creating the language, compiler, and tooling behind it. Thank you for your time Hongbo!
The original target of MoonBit is Wasm. WebAssemblyâs original promise was that many languages could be compiled to it and then run in the browser or other environments. The problem, however, is that existing languages such as Java and Go generate a huge amount of WebAssembly code when compiled, even when theyâre just printing âHello Worldâ. Rust generates small amount of code but it has steep learning curve for average programmer
MoonBit was built to fully deliver the advantage of WebAssembly. We say WebAssembly is safe and fast, but when Go is compiled to WebAssembly, it generates a very large amount of WebAssembly code, and itâs not fast anymore because the language semantics donât match very well with the Golang, So we think thereâs a opportunity, a big opportunity, to have a language that is designed for the WebAssembly semantics. MoonBit generates a code which is performance-wise comparable to Rust, and it also generates even smaller Wasm code than Rust.
MoonBit is also exploring the possibility of an AI-native language toolchain.
MoonBit was created the same year that ChatGPT was released, in 2022. Since then, we have seen the potential of AI-assisted coding. Traditional programming languages (such as Python) are mostly designed to interact better with humans, while MoonBit is designed to communicate better with AI. The most common issues with AI-assisted code are security concerns (trained on potentially untrusted source code) and the illusion problem (the inability to understand context). MoonBit's AI-friendly language design aims to reduce these issues through advanced static analysis.
As a developer platform, MoonBit deeply integrates its toolchain with AI. Our IDE has a built-in AI assistant for generating code, documentation, and tests, fixing errors, and suggesting code improvements. MoonBit's AI-native language + toolchain design is framing AI coding in a bottom-up way.
In many ways, MoonBit takes inspiration from Rust and Go.
It has pattern matching, static types and type inference, for example. MoonBit is a strongly typed language, like Rust, Kotlin and Java, which means it enforces strict rules about data types. MoonBit took the good parts of Rust, and we try to make it easy to learn.
Where MoonBit diverges from Rust is that it ships with a garbage collector, which uses reference counting (RC) similar to Swiftâs approach to garbage collecting. MoonBit uses state-of-the-art optimization techniques to eliminate the overhead of reference counting at compile time. So in memory heavy benchmarks, MoonBit performs similarly and sometimes even faster than high performance tracing garbage collector, significantly faster than Swift. This allows it to do automatic memory management.
Another advantage is that MoonBit has very fast compilation. One pain point of Rust is it takes a very long time to compile. MoonBit can compile the whole code in a very fast time like one or two orders of magnitude faster than the Rust compilation.
The way MoonBit influenced by Golang is from the philosophy level: Keep the language simple and invest more in the tooling side. Go has a philosophy of less is more, and we think this is quite important. You have to make the language itself cohesive. We donât want to continue adding to the syntax.
Before MoonBit, I was the core contributor to the OCaml, created ReScript, and worked on Flow at Meta. For creating production-ready programming languages: