Precision in type system design (or the Option type as an anti-pattern)

This article is an abridged version of my ElixirConf EU 2026 keynote.

As you may have heard, we have been working on a set-theoretic type system for Elixir. I have given several presentations and written many articles on the topic, and a recurring theme in those discussions is the subject of precision.

Take the following negate function in Elixir, which negates an integer or a boolean:

def negate(int) when is_integer(int), do: -int
def negate(bool) when is_boolean(bool), do: not bool

One may give it the type signature of integer() or boolean() -> integer() or boolean(). While this type signature is correct, it is imprecise, because it says that, if we give it an integer(), it may return an integer() or a boolean(), but in practice it only returns integers.

A more precise type signature is:

$ integer() -> integer()
$ boolean() -> boolean()
def negate(int) when is_integer(int), do: -int
def negate(bool) when is_boolean(bool), do: not bool

Elixir’s type system allows you to give multiple type signatures to a given function, which closely matches the idiom of multiple clauses in Elixir. This type signature is more precise because it captures that, if you give it an integer, it will return an integer and only an integer. The same for booleans.

When fine-tuning a type system for precision, we often have to make trade-offs between simplicity and expressiveness in its design. The goal of this article is to explore these trade-offs. While we will be using Elixir as an example, the majority of the insights here apply to mainstream languages.

At Dashbit, we help startups and enterprises adopt and grow their Elixir teams. We work with a limited number of clients and use our shared experiences to improve the Elixir ecosystem. Get in touch to learn how our team of experts can take your use of Elixir to the next level.

Example: precision in lists (or lack thereof)

Take the following Elixir code, which returns the longest word in a string:

# Returns longest word in "foo,bar,baaz,etc"
$ binary() -> binary()
def longest_word(sentence) when is_binary(sentence) do
  parts = String.split(sentence, ",")
  Enum.max_by(parts, &String.length/1)
end

String.split returns a list of binaries in Elixir. Lists are the main collection type in Elixir. Other languages may model them as arrays or vectors. In Elixir, Enum.max_by will raise an exception if it is given an empty list. Assuming a valid sentence is given as argument, does this code raise at runtime?

The answer is no, because String.split never returns an empty list. If you give "foo" and split on ",", then it returns ["foo"]. However, if your type signatures cannot distinguish between empty lists and non-empty lists, Enum.max_by cannot statically determine if you are giving it an empty list or not. Therefore, you have the following options:

  1. Enum.max_by has the type signature list(a), (a -> integer()) -> a and it will raise if given an empty list. Many avoid this option, as it implicitly raises runtime errors

  2. Enum.max_by has the type signature list(a), (a -> integer()) -> {:ok, a} or :error and it will return :error if given an empty list, which is similar to the Option/Maybe types found in many typed languages

If we choose the second option, we will need to adjust the longest_word implementation to unwrap the :ok/:error cases:

# Returns longest word in "foo,bar,baaz,etc"
def longest_word(sentence) when is_binary(sentence) do
  parts = String.split(sentence, ",")

  case Enum.max_by(parts, &String.length/1) do
    {:ok, word} -> word
    :error -> raise "this should not be possible"
  end
end

Since we know parts cannot ever be an empty list, the :error case is never possible, and we chose to raise (or panic or whatever your programming language of choice calls it).

The trouble with this approach is, because the type system cannot differentiate between empty and non-empty lists, the encoding that parts is a non-empty list has been moved to runtime. And, for most programming languages, this behaviour is not reflected anywhere in the types (by definition).

So imagine that in the future you break this function apart:

$ binary() -> binary()
def longest_word(sentence) do
  parts = String.split(sentence, ",")
  max_word(parts)
end

$ list(binary()) -> binary()
defp max_word(parts) when is_list(parts) do
  # Still implicitly assumes parts is a non-empty list
  case Enum.max_by(parts, &String.length/1) do
    {:ok, max} -> max
    :error -> raise "this should not be possible"
  end
end

And later someone else introduces a new caller to max_word:

$ binary() -> binary()
def longest_non_empty_word(sentence) do
  sentence
  |> String.split(",")
  |> Enum.reject(& &1 == "")
  |> max_word()
end

longest_non_empty_word can now actually raise at runtime for strings like "" or ",", because we remove all empty strings, but the type system will not emit a typing violation. In other words, since our type signatures did not distinguish between empty lists and non-empty lists, we enforce these requirements at runtime, leading to bugs potentially going unnoticed by the type system.

Distinguishing empty and non-empty lists

Let’s imagine, on the other hand, that our type signatures can separate empty from non-empty lists. This is actually how lists are defined in Elixir’s actual type system:

type list(a) = empty_list() or non_empty_list(a)

Furthermore, Elixir has union types (rather than tagged sum types), so non_empty_list(a) is a subtype of list(a) and we can define a type signature for Enum.max_by that matches exclusively on non-empty lists:

$ non_empty_list(a), (a -> integer()) -> a
def max_by(parts, function)

This is different from languages such as Haskell and Scala, which offer non-empty lists as a separate data type. Since they are not subtypes of regular lists, you must explicitly convert lists into non-empty lists, and such conversions return… an Option type, moving the check back to runtime.

And now our longest_word implementation can remain written as:

# Returns longest word in "foo,bar,baaz,etc"
$ binary() -> binary()
def longest_word(sentence) when is_binary(sentence) do
  parts = String.split(sentence, ",")
  Enum.max_by(parts, &String.length/1)
end

Now, when we refactor the function above and introduce longest_non_empty_word, we will get a typing violation, because max_word will either explicitly declare or have its signature inferred to support only non-empty lists:

$ binary() -> binary()
def longest_non_empty_word(sentence) do
  sentence
  |> String.split(",")
  |> Enum.reject(& &1 == "")
  |> max_word()
end

# Inferred as: non_empty_list(binary()) -> binary()
defp max_word(parts) do
  Enum.max_by(parts, &String.length/1)
end

This version is more concise, better encapsulates our intent, and has no needless raises (nor panics). Perhaps the logical extrapolation is that we want our type systems to be as expressive as possible to allow us to write precise type signatures?

The trouble with precision

The trouble with precision is that the more precise we make the type system, the more we have to prove in our own programs. Let’s continue with the assumption that Enum.max_by specifies in its type signature that it accepts only non-empty lists. Now take this code:

$ binary() -> binary()
def longest_word_from_last_10(sentence) do
  parts = String.split(sentence, ",")

  parts
  |> Enum.drop(max(length(parts) - 10, 0))
  |> Enum.max_by(&String.length/1)
end

The function above gets the longest word from the last 10 entries in a sentence, dropping anything before the last ten. However, it only drops anything that exceeds the last 10. In other words, Enum.max_by is never called at runtime with an empty list, but proving that’s the actual case requires the type system to track list lengths, which is quite hard! Therefore, the function above would emit a typing violation, even though the code does not raise at runtime. One way to address the violation is to introduce another “needless” raise:

$ binary() -> binary()
def longest_word_from_last_10(sentence) do
  parts = String.split(sentence, ",")

  case Enum.drop(parts, max(length(parts) - 10, 0)) do
    [] -> raise "this should not be possible"
    parts -> Enum.max_by(parts, &String.length/1)
  end
end

Furthermore, the more we increase the precision of the type system itself, the more verbose our type signatures become. For example, similar to list(a) = empty_list() or non_empty_list(a), imagine if we also break the types integer() and boolean() into the types below:

type integer() = neg_integer() or 0 or pos_integer()
type boolean() = true or false

This added precision means we can type our original negate function as follows:

$ pos_integer() -> neg_integer()
$ neg_integer() -> pos_integer()
$ 0 -> 0
$ true -> false
$ false -> true
def negate(int) when is_integer(int), do: -int
def negate(bool) when is_boolean(bool), do: not bool

The type definition is more precise but also much more verbose! Or imagine you have a max function that returns the maximum between two integers. It can now be typed as follows:

$ neg_integer(), neg_integer() -> neg_integer()
$ neg_integer(), 0 -> 0
$ neg_integer(), pos_integer() -> pos_integer()
$ 0, neg_integer() or 0 -> 0
$ 0, pos_integer() -> pos_integer()
$ pos_integer(), integer() -> pos_integer()
def max(a, b)

More precise types can lead to more verbose type signatures and harder-to-read error messages, and they can also negatively impact performance.

Simplicity or expressiveness?

In the presentation, I argue there is no silver bullet and there is no obvious choice between simplicity and expressiveness. Language designers have to balance the language idioms, ergonomics, performance, false positives, and more. However, I believe there are takeaways from this discussion that apply to the majority of the languages used today.

The Option type as an anti-pattern

Many programming languages have types similar to the :ok/:error patterns used in this article. They are often called Option/Maybe types, or Result/Either types when they also carry the reason for the failure. And we often use them to model behaviour that cannot be captured statically by the type system. Sometimes that’s intentional. Sometimes their usage hints at limitations of a given API or of the underlying type system.

For example, whenever you access a position in an array (or a tuple) that is known statically, and the function returns an Option type, it implies the type system (or the API) is not precise enough to model said operation. On the other hand, a precise type system would be able to statically guarantee whether accessing the second element of an array will succeed or fail.

As we saw with Enum.max_by, any guarantee that cannot be proven statically requires us to move its enforcement to the runtime. In this sense, the Option type is an anti-pattern, not for users, but for language designers and API authors, as it reveals where our design lacks precision.

However, not everything can be captured statically, and using said types is appropriate. Reading a file has to return an Option-like type, because you cannot statically know if a given file will be available at runtime.

Type systems do not mean “error free”

While type systems can rule out certain classes of errors, no mainstream type system can fully model the runtime semantics of our programs. Errors can happen at the arithmetic level (division by zero, integer overflows), at the data structure level (accessing an element of an array), at the I/O level (reading a file, writing to a socket), and more.

Most mainstream programming languages have exceptions, panics, and similar control-flow constructs too, kept separate from the type system.

The combination of these two characteristics means that the bugs we have seen in the previous sections will actually go unnoticed by most type systems. Modules and libraries will encode assumptions at runtime, which may be violated at some point in time, leading to crashes. This means that we must still write our programs with the assumption that any individual component may fail, and isolate those failures so they do not take down the rest of the system. Otherwise, a single .unwrap() (Rust’s equivalent of our raise "this should not be possible") may leave you with a global outage on your hands.

In other words, Elixir’s processes, supervision trees, and other techniques for isolating failures and building fault-tolerant systems remain strictly necessary, as type systems do not effectively prove our programs are error free.

Type systems will always reject valid programs

No matter how expressive you make a type system, as long as it is sound and decidable, it will always reject valid programs.

Even though Elixir’s type system distinguishes between empty and non-empty lists, the type system still cannot prove the longest_word_from_last_10 function to be correct:

$ binary() -> binary()
def longest_word_from_last_10(sentence) do
  parts = String.split(sentence, ",")

  parts
  |> Enum.drop(max(length(parts) - 10, 0))
  |> Enum.max_by(&String.length/1)
end

Of course, we could add more features, such as tracking list lengths, but there will always be a more complex program that will be rejected, even though it would not crash at runtime.

Our goal with the Elixir type system is to balance all these trade-offs in ways that feel natural to Elixir developers, using the existing language idioms as our guiding light.

Addendum: does AI change the conclusion?

When I gave this presentation in April 2026, the question of simplicity vs expressiveness was left open-ended, given the impact these choices have on developer ergonomics. However, do coding agents care if a type signature spans several lines? Or if an error message has long traces? Which choices should we make if we were to optimize for coding agents instead?

That’s another question I don’t have an answer for, but it would also be unwise to dismiss it.

On the positive side, Elixir’s type system allows us to add precision after the fact. Most type systems cannot simply split Int into separate types for negative integers, zero, and positive integers without breaking every program out there. But, since Elixir relies on (untagged) unions, it could split integer() into pos_integer() or 0 or neg_integer() with no breaking changes. Warnings would be emitted only when functions later refine their signatures to accept a subset of those types, helping uncover bugs while also leading to false positives. This means we can afford to make such decisions further down the road, regardless of where the demand for more precision comes from.

The type system was made possible thanks to a partnership between CNRS and Remote. The development work is currently sponsored by Fresha, Supermethod and Dashbit.