Safe Resource Management
DETS tables must be properly closed to ensure data is flushed to disk. If a table is not closed — for example, because an error occurs — pending writes may be lost and the file may need repair on next open.
with_table helps with short-lived operations by opening a table, running your callback, and closing the table before it returns. If the callback raises, with_table still attempts to close the table before re-raising the exception.
Basic usage
Section titled “Basic usage”Instead of manually opening and closing:
import gleam/dynamic/decodeimport slate/set
// ❌ Manual lifecycle — close might not be called if an error occurslet assert Ok(table) = set.open("data/config.dets", key_decoder: decode.string, value_decoder: decode.string)let assert Ok(Nil) = set.insert(table, "theme", "dark")let assert Ok(Nil) = set.close(table)Use with_table:
import gleam/dynamic/decodeimport slate/set
// ✅ Table is closed when the callback completeslet assert Ok(Nil) = set.with_table("data/config.dets", key_decoder: decode.string, value_decoder: decode.string, fun: fn(table) { set.insert(table, "theme", "dark") })Using use syntax
Section titled “Using use syntax”Gleam's use syntax makes with_table even cleaner:
import gleam/dynamic/decodeimport slate/set
let result = { use table <- set.with_table("data/config.dets", key_decoder: decode.string, value_decoder: decode.string) let assert Ok(Nil) = set.insert(table, "theme", "dark") set.lookup(table, key: "theme")}// table is closed here once the block returnsReturn values
Section titled “Return values”with_table returns whatever your callback returns:
import gleam/dynamic/decodeimport slate/set
let assert Ok(age) = set.with_table("data/users.dets", key_decoder: decode.string, value_decoder: decode.int, fun: fn(table) { let assert Ok(Nil) = set.insert(table, "alice", 42) set.lookup(table, key: "alice") })// age == 42Error handling
Section titled “Error handling”If the callback returns an Error, with_table still attempts to close the table before returning the callback error:
import gleam/dynamic/decodeimport slate/set
let result = set.with_table("data/users.dets", key_decoder: decode.string, value_decoder: decode.int, fun: fn(table) { set.lookup(table, key: "nonexistent") })// result == Error(NotFound), and the table has been closedIf the callback raises, with_table still attempts to close the table before re-raising:
import gleam/dynamic/decodeimport slate/set
let _ = set.with_table("data/users.dets", key_decoder: decode.string, value_decoder: decode.int, fun: fn(table) { let assert Ok(Nil) = set.insert(table, "alice", 42) panic as "boom" })If the table itself fails to open, the error is returned immediately:
import gleam/dynamic/decodeimport slate/set
let result = set.with_table("corrupted.dets", key_decoder: decode.string, value_decoder: decode.string, fun: fn(table) { set.lookup(table, key: "key") })// result == Error(...) from the open failureAvailable on all table types
Section titled “Available on all table types”with_table is available on all three table types:
import gleam/dynamic/decodeimport slate/setimport slate/bagimport slate/duplicate_bag
let assert Ok(_) = set.with_table("data/set.dets", key_decoder: decode.string, value_decoder: decode.string, fun: fn(table) { ... })let assert Ok(_) = bag.with_table("data/bag.dets", key_decoder: decode.string, value_decoder: decode.string, fun: fn(table) { ... })let assert Ok(_) = duplicate_bag.with_table("data/dup.dets", key_decoder: decode.string, value_decoder: decode.string, fun: fn(table) { ... })Repair and access options
Section titled “Repair and access options”with_table always opens the table with AutoRepair and ReadWrite access. If you need a different repair policy or read-only access, use open_with or open_with_access directly and manage the lifecycle yourself.
When to use with_table
Section titled “When to use with_table”| Scenario | Recommended |
|---|---|
| Quick lookup or insert | with_table |
| Script that reads/writes once | with_table |
| Long-running server with a persistent cache | open / close |
| Multiple operations across time | open / close |