Skip to content

Commit 826cc51

Browse files
committed
first pass at regex and dict native syntax + type spread pattern match blog post
1 parent 0c8a847 commit 826cc51

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# ReScript v12 — Simpler, Safer, Smarter
2+
3+
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**.
4+
5+
Each of these features makes ReScript smoother to write, easier to read, and a lot more fun to work with.
6+
7+
---
8+
9+
## 1. First-Class Regex
10+
11+
ReScript v12 introduces **native regex literals**, using the same syntax as JavaScript:
12+
13+
```res
14+
let pattern = /foo-bar(\d+)/g
15+
let result = String.match("foo-bar123 and foo-bar456", pattern)
16+
```
17+
18+
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.
19+
20+
In short: the power of JS regex, now fully typed and built right in.
21+
22+
---
23+
24+
## 2. `dict{}` Literals and Pattern Matching
25+
26+
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:
27+
28+
```res
29+
let d = dict{"A": 5, "B": 6}
30+
31+
let maybeB = d->Dict.get("B")
32+
33+
let result = switch d {
34+
| dict{"B": b, "C": ?c} => Some((b, c))
35+
| _ => None
36+
}
37+
```
38+
39+
- `dict{...}` creates a dictionary literal.
40+
- `dict{"B": b}` extracts a specific key’s value.
41+
- `?c` marks a key as optional, returning an `option` type.
42+
43+
Whether you’re dealing with JSON, metadata, or flexible configuration objects, this syntax keeps things clean and type-safe.
44+
45+
---
46+
47+
## 3. Variant Type Spreads
48+
49+
Variants (sum types) are among ReScript’s most powerful features — and they just leveled up.
50+
51+
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:
52+
53+
```res
54+
type pets = Cat | Dog
55+
type fish = Cod | Salmon
56+
type animals = | ...pets | ...fish
57+
58+
let greet = (a: animals) =>
59+
switch a {
60+
| ...pets as pet => greetPet(pet)
61+
| ...fish as f => greetFish(f)
62+
}
63+
```
64+
65+
It’s now easier than ever to compose, extend, and organize your types — without sacrificing clarity or exhaustiveness.
66+
67+
---
68+
69+
## Wrapping Up
70+
71+
ReScript v12 focuses on developer happiness: small changes that make a big difference in daily coding.
72+
73+
| Feature | What It Brings |
74+
| ------------------------------- | ---------------------------------------- |
75+
| **Regex literals** | Native syntax, no interop needed |
76+
| **`dict{}`\*\*** + matching\*\* | Safer, more expressive dynamic data |
77+
| **Variant spreads** | Reusable and composable type hierarchies |
78+
79+
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.
80+
81+
---
82+
83+
### Final Thought
84+
85+
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

Comments
 (0)