|
| 1 | +--- |
| 2 | +author: rescript-team |
| 3 | +date: "2025-11-18" |
| 4 | +badge: roadmap |
| 5 | +title: "Refined regex, dicts and variant type spreads" |
| 6 | +description: | |
| 7 | + ReScript 12 comes with plenty of quality-of-life improvements like native regex and dict syntax and refined variant variant type spread. |
| 8 | +--- |
| 9 | + |
| 10 | +# ReScript v12 — Simpler, Safer, Smarter |
| 11 | + |
| 12 | +ReScript 12 has landed with a fresh batch of quality-of-life improvements that make your code cleaner, safer, and more expressive. The stars of this release: **first-class regex**, **native dictionaries with pattern matching**, and **refined variant type spreads**. |
| 13 | + |
| 14 | +Each of these features makes ReScript smoother to write, easier to read, and a lot more fun to work with. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## 1. First-Class Regex |
| 19 | + |
| 20 | +ReScript v12 introduces **native regex literals**, using the same syntax as JavaScript: |
| 21 | + |
| 22 | +```res |
| 23 | +let pattern = /foo-bar(\d+)/g |
| 24 | +let result = String.match("foo-bar123 and foo-bar456", pattern) |
| 25 | +``` |
| 26 | + |
| 27 | +It feels just like JS — but with ReScript’s type safety on your side. Perfect for validating user input, parsing filenames, or searching through text, without having to rely on interop wrappers. |
| 28 | + |
| 29 | +In short: the power of JS regex, now fully typed and built right in. |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +## 2. `dict{}` Literals and Pattern Matching |
| 34 | + |
| 35 | +Meet `dict{}` — ReScript’s new, native syntax to handle dynamic key/value data. Define and destructure dictionaries with the same simplicity and safety you expect from the language: |
| 36 | + |
| 37 | +```res |
| 38 | +let d = dict{"A": 5, "B": 6} |
| 39 | +
|
| 40 | +let maybeB = d->Dict.get("B") |
| 41 | +
|
| 42 | +let result = switch d { |
| 43 | +| dict{"B": b, "C": ?c} => Some((b, c)) |
| 44 | +| _ => None |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +- `dict{...}` creates a dictionary literal. |
| 49 | +- `dict{"B": b}` extracts a specific key’s value. |
| 50 | +- `?c` marks a key as optional, returning an `option` type. |
| 51 | + |
| 52 | +Whether you’re dealing with JSON, metadata, or flexible configuration objects, this syntax keeps things clean and type-safe. |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## 3. Variant Type Spreads |
| 57 | + |
| 58 | +Variants (sum types) are among ReScript’s most powerful features — and they just leveled up. |
| 59 | + |
| 60 | +Before v12, you could reuse constructors from another variant with type spread. That was already handy, but often you also wanted to reuse the functions tied to those base types. Starting in v12, you can pattern-match and cast to the base types all at once, with a clean new syntax: |
| 61 | + |
| 62 | +```res |
| 63 | +type pets = Cat | Dog |
| 64 | +type fish = Cod | Salmon |
| 65 | +type animals = | ...pets | ...fish |
| 66 | +
|
| 67 | +let greet = (a: animals) => |
| 68 | + switch a { |
| 69 | + | ...pets as pet => greetPet(pet) |
| 70 | + | ...fish as f => greetFish(f) |
| 71 | + } |
| 72 | +``` |
| 73 | + |
| 74 | +It’s now easier than ever to compose, extend, and organize your types — without sacrificing clarity or exhaustiveness. |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## Wrapping Up |
| 79 | + |
| 80 | +ReScript v12 focuses on developer happiness: small changes that make a big difference in daily coding. |
| 81 | + |
| 82 | +| Feature | What It Brings | |
| 83 | +| ------------------------------- | ---------------------------------------- | |
| 84 | +| **Regex literals** | Native syntax, no interop needed | |
| 85 | +| **`dict{}`\*\*** + matching\*\* | Safer, more expressive dynamic data | |
| 86 | +| **Variant spreads** | Reusable and composable type hierarchies | |
| 87 | + |
| 88 | +Whether you’re parsing data, modelling complex states, or cleaning up old interop code, these new tools help you write expressive, reliable ReScript with less effort. |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +### Final Thought |
| 93 | + |
| 94 | +ReScript 12 isn’t just an update — it’s a smoother, friendlier version of the language you already love. Give it a try, and you’ll feel the difference right away. |
0 commit comments