Gleam is a [[Apache v2]] licensed bytecode interpreted ([[BEAM]]) language whose compiler is written in [[Rust]].
- [Website](https://gleam.run/)
- [Source](https://github.com/gleam-lang/gleam)
- [Documentation](https://gleam.run/documentation/)
> The power of a type system, the expressiveness of functional programming, and the reliability of the highly concurrent, fault tolerant Erlang runtime, with a familiar and modern syntax.
# Notability
It's another [[BEAM]] language like [[Elixir]] and [[Erlang]], but weirdly written in [[Rust]].
# Philosophy
> As a community, we want to be friendly too. People from around the world, of all backgrounds, genders, and experience levels are welcome and respected equally. See our community code of conduct for more.
>
> Black lives matter. Trans rights are human rights. No nazi bullsh*t.
\- Official Gleam Website
## Aesthetics and Interop
Prioritizes [[Javascript]] interop, which is weird for a language that ostensibly targets [[BEAM]]. I assume that is the reason they went with [[Rust]] as the compiler language, as Rust is one of the few languages with a well known [[WebAssembly]] compilation feature.
The syntax is [[Rust]]-like rather than [[Ruby]]-like and I assume the waning popularity of Ruby and the waxing popularity of Rust are major motivations for Gleam.
# OS Support
# Features
## Type Annotations
Unlike other [[BEAM]] languages, Gleam actually supports first-class type specifications for simple variables:
```gleam
let x: Int = 1
```
> These annotations are optional and while they are checked, they do not aid the type checker. Gleam code is always fully type checked with or without type annotations.
## Block Expressions
In a very Ruby-like move, all curly brace blocks in Gleam are treated as compound expressions and return the result of the last expression evaluated.
```gleam
let value: Bool = {
"Hello"
42 + 12
False
} // => False
```
Interestingly, because blocks are evaluated immediately, they can be used to indicate the order of operations in nested expressions.
> Expression blocks are used instead of parentheses to change the precedence of operations.
## Namespaces
```gleam
import unix/cat
import animal/cat as kitty
```
## Errors
> Gleam doesn't have exceptions or `null` to represent errors in our programs, instead we have the `Result` type. If a function call fails, wrap the returned value in a `Result`, either `Ok` if the function was successful, or `Error` if it failed.
```gleam
// type definition
pub type Result(value, reason) {
Ok(value)
Error(reason)
}
```
```gleam
pub type MyDatabaseError {
InvalidQuery
NetworkTimeout
}
pub fn insert(db_row) {
// ... something went wrong connecting to a database here
Error(NetworkTimeout)
}
```
## Panic & Todo
- `todo` will print a warning during compilation
- `panic` will crash the program at runtime
```gleam
fn favourite_number() -> Int {
todo as "We're going to decide which number is best tomorrow"
}
fn unreachable_function() -> Int {
panic as "this function should never be called"
}
```
## `use` Expressions
These two examples are equivalent:
```gleam
pub fn main() {
logger.record_timing(fn() {
database.connect(fn(db) {
file.open("file.txt", fn(f) {
// Do something with `f` here...
})
})
})
}
```
```gleam
pub fn main() {
use <- logger.record_timing
use db <- database.connect
use f <- file.open("file.txt")
// Do something with `f` here...
}
```
Absolutely wild workaround for the module-first design shared by [[BEAM]] languages. It reduces nesting but the order of operations is not preserved.
# Tips
# References