defer

A simple library for adding defer functionality to gleam.

Package Version Hex Docs

Installation

gleam add defer

Usage

defer is easy to use! (ha, ha get it?)

import defer

pub fn main() {
  use <- defer(fn() { io.println("1") })
  use <- defer(fn() { io.println("2") })
  use <- defer(fn() { io.println("3") })
  io.println("4")
}
4
3
2
1

Note that the defers execute in reverse! If you don’t care about the order of your defers then this is fine. However, If you would like the defers to execute in order then use start_defer instead.

import defer

pub fn main() {
  use ctx <- start_defer(fn() { io.println("1") })
  use ctx <- add_defer(ctx, fn() { io.println("2") })
  use ctx <- add_defer(ctx, fn() { io.println("3") })
  #(ctx, io.println("4"))
}
4
1
2
3

Further documentation can be found at https://hexdocs.pm/defer.

Development

This library is still a work in progress so feel free to suggest ways to make the api more ergonomic and features that should be added.

gleam test  # Run the tests
gleam shell # Run an Erlang shell
Search Document