Alex Spinov Gleam compiles to both Erlang BEAM and JavaScript. You get Erlang's legendary fault tolerance with a...
Gleam compiles to both Erlang BEAM and JavaScript. You get Erlang's legendary fault tolerance with a modern, friendly type system.
import gleam/io
pub fn main() {
io.println("Hello, Gleam!")
}
gleam new my_app
cd my_app && gleam run
// Custom types (like Rust enums)
pub type Shape {
Circle(radius: Float)
Rectangle(width: Float, height: Float)
Triangle(base: Float, height: Float)
}
pub fn area(shape: Shape) -> Float {
case shape {
Circle(r) -> 3.14159 *. r *. r
Rectangle(w, h) -> w *. h
Triangle(b, h) -> 0.5 *. b *. h
}
}
// Compiler ensures ALL cases are handled
import gleam/result
pub type AppError {
NotFound
Unauthorized
DatabaseError(String)
}
pub fn get_user(id: Int) -> Result(User, AppError) {
case db.find_user(id) {
Ok(user) -> Ok(user)
Error(_) -> Error(NotFound)
}
}
// Chain operations
pub fn get_user_email(id: Int) -> Result(String, AppError) {
use user <- result.try(get_user(id))
use profile <- result.try(get_profile(user))
Ok(profile.email)
}
The use keyword makes error handling clean — no nested case statements.
import gleam/erlang/process
import gleam/otp/actor
pub type Message {
Increment
GetCount(process.Subject(Int))
}
pub fn counter() {
actor.start(0, fn(message, count) {
case message {
Increment -> actor.continue(count + 1)
GetCount(reply_to) -> {
process.send(reply_to, count)
actor.continue(count)
}
}
})
}
Actors, supervisors, fault tolerance — all the OTP power.
import wisp
import gleam/http
pub fn handle_request(req: wisp.Request) -> wisp.Response {
case wisp.path_segments(req) {
[] -> wisp.ok() |> wisp.string_body("Hello!")
["users", id] -> get_user_handler(req, id)
_ -> wisp.not_found()
}
}
fn get_user_handler(req: wisp.Request, id: String) -> wisp.Response {
case req.method {
http.Get -> {
// Fetch and return user
wisp.ok() |> wisp.json_body(user_json)
}
_ -> wisp.method_not_allowed([http.Get])
}
}
# Target JavaScript instead of Erlang
gleam build --target javascript
gleam run --target javascript
Share types and logic between your BEAM backend and JS frontend.
Interested in robust backend systems? I build developer tools and data infrastructure. Email spinov001@gmail.com or check my Apify tools.