← Cookbook Interop Intermediate · Pooka 0.5.0

Use an Elixir or Gleam package

Hex is shared by the whole BEAM. Add an Elixir package and a Gleam one, describe the functions you need, and call them like neighbours.

You will use

  • pooka add slugify (Elixir) and pooka add gleam_stdlib (Gleam)
  • external Name from elixir "Module" { … }
  • external Name from gleam "gleam/string" { … }

The whole program

src/main.pooka
module main

import pooka/io

/// From Elixir: the `slugify` package.
external Slug from elixir "Slug" {
  slugify(text: String) -> String
}

/// From Gleam: the standard library's string functions.
external GleamString from gleam "gleam/string" {
  reverse(text: String) -> String

  capitalise(text: String) -> String
}

/// A web address for a page title.
slug(title: String) -> String {
  Slug.slugify(title)
}

main() {
  io.println(slug("A Lantern in the Night Library!"))
  io.println(GleamString.capitalise("pooka meets gleam"))
  io.println(GleamString.reverse("lantern 🏮"))
}

Run it

Add both packages first; they land in pooka.toml and pooka.lock:

$ pooka add slugify
$ pooka add gleam_stdlib
$ pooka run
a-lantern-in-the-night-library
Pooka meets gleam
🏮 nretnal

How it works

  1. Elixir modules have capitalised names. Elixir's Slug is what goes after from elixir. Pooka builds Elixir packages with Mix, so Elixir needs to be installed; if it is not, Pooka says so.
  2. Gleam modules have paths. In Gleam, gleam/string is a module path, and that is exactly what goes after from gleam. Gleam packages arrive already compiled to Erlang, so they need nothing extra.
  3. The types line up. Strings are UTF-8 binaries in all three languages, so a Pooka String crosses into Elixir and Gleam and back without any translation. Gleam's Result and Option even look exactly like Pooka's.
  4. Wrap what you use. slug is an ordinary Pooka function. The rest of the program calls it and never needs to know which language did the work.

Erlang packages from Hex work the same way: pooka add them and use from erlang, as in Call an Erlang function. The BEAM interop guide shows how each Pooka value looks on the other side.