Grain (language)
By the Grain project
Grain is an open-source, statically typed functional programming language designed to compile directly to WebAssembly, aiming to give developers a modern, ML-family language experience purpose-built for the WebAssembly runtime rather than…
Definition
Grain is an open-source, statically typed functional programming language designed to compile directly to WebAssembly, aiming to give developers a modern, ML-family language experience purpose-built for the WebAssembly runtime rather than adapted to it.
Overview
WebAssembly (Wasm) emerged as a portable, near-native-speed compilation target for the web and beyond, but most languages that target it — C, Rust, Go, AssemblyScript — were designed for other environments first and adapted to Wasm afterward. Grain was created specifically as a WebAssembly-native language from the start, meaning its runtime, memory model, and standard library are designed around Wasm's execution model rather than being retrofitted onto it. Syntactically and semantically, Grain draws heavily from the ML family of languages (OCaml, ReasonML, Elm), offering static typing with type inference, pattern matching, algebraic data types, and immutability by default, wrapped in a syntax intended to feel familiar to developers coming from JavaScript or other C-like languages. Grain's compiler performs its own garbage collection and memory management within the compiled Wasm module, so Grain programs are self-contained and do not require a separate host-managed runtime for memory safety, distinguishing it from lower-level Wasm targets like C or Rust where memory management is manual or ownership-based. Because Grain compiles to standard WebAssembly, Grain programs can run in any Wasm-compatible environment, including browsers, WASI-based server runtimes, and edge computing platforms, and can interoperate with JavaScript and other Wasm modules. The project remains a smaller, community-driven effort relative to established Wasm targets, and is generally used by developers specifically interested in functional programming on WebAssembly, in language design experimentation, or in exploring what a language built ground-up for Wasm's constraints (linear memory, no direct DOM access, sandboxing) can offer compared to adapted general-purpose languages.
Key Features
- Statically typed functional language designed specifically for WebAssembly
- ML-family semantics: type inference, pattern matching, algebraic data types
- Immutable-by-default data structures
- Self-contained memory management and garbage collection compiled into Wasm output
- Familiar, JavaScript-influenced syntax over ML-style semantics
- Runs anywhere WebAssembly runs: browsers, WASI runtimes, edge platforms
- Open-source and community-driven development model
- Interoperates with JavaScript and other WebAssembly modules
Use Cases
Alternatives
Frequently Asked Questions
From the Blog
Analytical SQL: Patterns for Turning Tables Into Answers
Analytical SQL becomes reliable when you treat it as a sequence of patterns rather than one clever query. This guide walks the repeatable steps — define the grain, filter, aggregate, window, validate — and shows how grain mistakes, NULL semantics and hidden fan-out produce numbers that look plausible and are wrong.
Read More Data Sciencedbt test severity: when a failing test should warn and when it should break the build
Tier your dbt tests by what they guarantee. Tests that protect a key or a grain error and block the build. Tests that describe expected but not guaranteed data shape warn with a threshold. Freshness gets its own tier. A suite everyone ignores is worse than a smaller suite that is always green.
Read More Data ScienceWhy your SQL join inflates the totals, and how to catch it
Inflated totals after adding a join are always a grain violation: the joined table is not unique on the join key, so rows fan out and every sum is multiplied. Check uniqueness before joining, guard the row count across the join, and fix it by aggregating first, using a semi join, or redefining the metric.
Read More