Language Tour
Basics
Section titled “Basics”let name = "Alice"
let greet(name: string) -> string = { `Hello, ${name}!`}
let identity<T>(x: T) -> T = { x }let result = [1, 2, 3, 4, 5] |> filter((n) -> n > 2) |> map((n) -> n * 10) |> sort
users |> filter(.active) |> map(.name) |> sort
5 |> add(3, _) // add(3, 5)let addTen = add(10, _) // partial applicationPattern Matching
Section titled “Pattern Matching”let label = match status { 200..299 -> "success", 404 -> "not found", _ -> "unknown",}
match route { Home -> <HomePage />, Profile(id) -> <ProfilePage id={id} />, NotFound -> <NotFoundPage />,}type User = { id: string, name: string, email: string }
type Shape = | Circle { radius: number } | Rectangle { width: number, height: number }
let updated = User { name: "Bob", ..u }type UserId = UserId(string) // newtypeError Handling
Section titled “Error Handling”let loadProfile(id: string) -> Result<Profile, Error> = { let user = fetchUser(id)? // ? returns Err early let posts = fetchPosts(user.id)?
Ok(Profile(user, posts))}
match user.nickname { Some(nick) -> nick, // Option<T> replaces null None -> user.name,}For Blocks & Traits
Section titled “For Blocks & Traits”for Array<Todo> { export let remaining(self) -> number = { self |> filter(.done == false) |> length }}
trait Display { let display(self) -> string }impl Display for User { let display(self) -> string = { `${self.name} (${self.email})` }}export let Counter() -> JSX.Element = { let (count, setCount) = useState(0)
<div> <h1>{`Count: ${count}`}</h1> <button onClick={() -> setCount(count + 1)}>+</button> </div>}Imports & Async
Section titled “Imports & Async”import { Todo } from "./types"import { parseYaml } from "yaml-lib" // untrusted by defaultlet data = parseYaml(input)? // auto-wrapped in Result, ? unwrapsimport trusted { useState } from "react" // trusted = direct call, no wrappingimport { for Array } from "./helpers" // import for-block extensions
let fetchUser(id: string) -> Promise<Result<User, Error>> = { let response = Http.get(`/api/users/${id}`) |> Promise.await? let user = response |> Http.json |> Promise.await?
Ok(user)}let add(a: number, b: number) -> number = { a + b }
test "addition" { assert add(1, 2) == 3 assert add(-1, 1) == 0}Placeholders
Section titled “Placeholders”let processPayment(order: Order) -> Result<Receipt, Error> = { todo }match direction { "north" -> go(0, 1), _ -> unreachable }