Skip to content

· 4 min read

Lobster borrows the argument

Lobster checks generic functions at their concrete calls, then uses the same context to work out ownership. A short function carries more information than its syntax suggests.

A terracotta lobster holds olive thread running from a large wooden spool toward two brass winders.
Illustration generated with GPT Image

A small helper often knows very little about the thing it receives. It checks a length, prints a name, or visits a few fields, then returns control to the caller. I like languages that let that helper stay small. Wouter van Oortmerssen's Lobster takes the idea further than its spare syntax first suggests: the caller helps decide both which types the helper sees and how its arguments stay alive.

Start with a function that asks for a length. A string and a vector can both answer, although they are different types. Here is the entire example I would use to introduce the mechanism.

One definition, concrete argument types
def count_units(values):    return length(values)print count_units("shell")print count_units([2, 4, 8])
  1. 1Leaving the parameter untyped makes this function generic.
  2. 2The function body is checked with a string argument.
  3. 3A vector argument gives the checker another concrete case.
A generic length helper. The string call counts bytes; the vector call counts elements.

The example explains how one definition can accept both inputs. It says nothing by itself about which reference-count operations survive compilation. That needs a look at the ownership rules, and eventually the generated program.

Check the call you have#

Lobster's type-system documentation describes specialization around concrete argument types, with checking following the call graph. Untyped parameters receive their types from callers. Explicit annotations can constrain them, and a named generic parameter can require two arguments to share a type. Flow typing also carries information from conditions: testing a nilable value lets the successful branch use its underlying type.

I find this an appealing order of explanation. Begin with two actual calls and ask whether the operations in the body make sense for each. A reusable helper can grow out of an ordinary piece of code. When the relationship between its arguments becomes important, I can write that relationship down.

There is a debugging tradeoff. The documentation warns that sparse annotations can move an error farther down the call chain. I would annotate the boundaries where I want mistakes to stop: a parser's result, a public function's input, perhaps the representation handed to a renderer. A little redundancy can make a compiler error much easier to place.

The same call carries a lifetime#

The memory-management design combines reference counting with ownership analysis. The compiler tries to borrow existing values for uses that need no additional owner. When another use needs ownership, it can insert a reference-count increase. Ownership analysis is interwoven with type checking because specialization can account for ownership as well as argument types.

For a helper that briefly reads a vector, borrowing matches the intent nicely. The caller already has a reason to keep the vector alive. Making every visit acquire another ownership share would add work whose purpose is already satisfied. The attractive part is how much the compiler can discover from an ordinary call.

Mutation still matters. The design describes conflicts when a later argument removes an element that an earlier argument borrows, as in a call shaped like f(v[0], v.pop()). Some other conflicts can be resolved by giving a value an owning reference. The programmer avoids explicit lifetime annotations, while the compiler still has to preserve the borrowed value's storage.

This is where I would slow down during an experiment. A helper that only reads is easy to reason about. A helper that stores its input, returns it, or changes the surrounding container deserves a closer look. I want to see which small edits change the ownership decision and whether the resulting diagnostics point at the operation I need to understand.

Count what remains#

The language reference gives small structs value semantics, stored inline and copied by value. Strings, vectors, classes, and resources use reference semantics. Reference-counted storage is released when its last reference disappears. Cycles need attention: they can keep each other alive, and Lobster reports remaining cycles at program exit.

My next step would be a small workload with both temporary reads and retained shared values. I would count remaining reference-count updates, specialized function bodies, executable size, and compile time. Specialization spends compiler work and code space to obtain context; the useful measurement is whether that purchase pays for this program. Lobster makes the bargain interesting by letting one compact helper participate in both type checking and memory management. I would like to see how far that compactness survives ordinary changes.