Functions & Const
Const Declarations
Section titled “Const Declarations”All bindings are immutable. Use const:
const name = "Floe"const count = 42const active = trueWith type annotations:
const name: string = "Floe"const count: number = 42Destructuring
Section titled “Destructuring”const [first, second] = getItems()const { name, age } = getUser()Functions
Section titled “Functions”fn add(a: number, b: number) -> number { a + b}The last expression in a function body is the return value. The return keyword is not used in Floe.
In multi-statement functions, floe fmt adds a blank line before the final expression to visually separate the return value:
fn loadProfile(id: string) -> Result<Profile, ApiError> { const user = fetchUser(id)? const posts = fetchPosts(user.id)? const stats = computeStats(posts)
Profile(user, posts, stats)}Exported functions must have return type annotations:
export fn greet(name: string) -> string { `Hello, ${name}!`}Default Parameters
Section titled “Default Parameters”fn greet(name: string = "world") -> string { `Hello, ${name}!`}Anonymous Functions (Closures)
Section titled “Anonymous Functions (Closures)”Use fn(x) for inline anonymous functions:
todos |> Array.map(fn(t) t.text)items |> Array.reduce(fn(acc, x) acc + x.price, 0)onClick={fn() setCount(count + 1)}For simple field access, use dot shorthand:
todos |> Array.filter(.done == false)todos |> Array.map(.text)users |> Array.sortBy(.name)const name = fn(x) ... is a compile error. If it has a name, use fn:
// COMPILE ERRORconst double = fn(x) x * 2
// correctfn double(x: number) -> number { x * 2 }Function Types
Section titled “Function Types”Use fn and -> to describe function types:
type Transform = fn(string) -> numbertype Predicate = fn(Todo) -> booleantype Callback = fn() -> ()Async Functions
Section titled “Async Functions”async fn fetchUser(id: string) -> Promise<User> { const response = await fetch(`/api/users/${id}`) await response.json()}What’s Not Here
Section titled “What’s Not Here”- No
letorvar- all bindings areconst - No
class- use functions and records - No
this- functions are pure by default - No
function*generators - use arrays and pipes - No
=>- usefn(x)for closures,->for types and match arms
These are removed intentionally. See the comparison for the reasoning.