Exr

The best programming language ever created. Designed for LLMs. Designed for stability. Always performant. Only slightly fastidious. Unapologetically extra.

Get Started

Modern cool features

Extra is a love letter to modern programming, with features like algebraic data types, type derivations, immutable data, and reactive views. The syntax is clean and thoroughly type-safe. No runtime errors and no try/catch, thank you very much.

enum Result<Success, Failure> {
  .ok(value: Success)
  .err(error: Failure)

  fn map<U>(
    # mapper: fn(# value: Success): U
  ): Result(U, Failure) =>
    switch this
    case .ok(value:)
      .ok(value: mapper(value))
    case .err(error:)
      .err(error:)
}

type User = {
  name: String
  phone: Array(Int)
  city: String
}
type AnonymousUser = Omit(User, 'name', 'phone')

view UserCard(user: User | AnonymousUser) =>
  <div class="card">
    {if user is User then <h1>{user.name}</h1>}
    <p>{user.city}</p>
  </div>

Declarative reactive programming that just works

React has had a great run as the world's most popular foot gun. Solid and Vue and Svelte are noble attempts to bring some sanity into the world, but if you're going to concede to JavaScript/TypeScript, then you're never going to be fully declarative.

In Extra, every change to state is a named message: msg increment => @count += 1. A message is a value, so a button takes onClick=increment, and a debugger can record each one with the state before and after. The runtime knows exactly which views depend on the state that changed, and updates only those, with no diffing (you can see the Elm inspiration here, taken in a new direction). This one runs in a terminal, on TeaUI.

import { Box, Stack, Button, Input, Badge } from TeaUI

view Counter {
  @count = 0
  @name = ''

  msg increment =>
    @count += 1

  msg decrement =>
    @count -= 1

  msg set-name(# value: String) =>
    @name = value

  render =>
    <Box border="rounded" padding=1>
      <Stack.down gap=1>
        Hello {@name}, count: {@count}
        <Stack.right gap=1>
          <Button title="-" onClick=decrement />
          <Button title="+" onClick=increment />
        </Stack.right>
        <Input
          value=@name
          placeholder="your name"
          onChange=set-name
        />
        {if @count >= 10 then <Badge text="double digits!" />}
      </Stack.down>
    </Box>
}

Refinement Types

Extra's refinement types let you define subsets of standard types (Array, Int, String) with compile-time-enforced rules. This is a natural, if unique, extension of thinking of types in terms of set theory. These refinements propagate through conditionals, enabling branch-specific type narrowing.

-- a port is between 0 and 65,535
type Port = Int(0..<65_536)
-- each segment of an IPv4 address is between 0 and 255
type Segment = Int(0..<256)
-- and there are exactly 4 of them
type IPv4 = Array(Segment, length: =4)
type Server = {ip: IPv4, port: Port}

fn server(segments: Array(Segment), port: Port): Server? =>
  if segments.length == 4 then
    -- the length was checked, so `segments` is an IPv4 here
    {ip: segments, port:}
  else
    null

fn local(): Server =>
  {ip: [192, 168, 1, 256], port: 80}
  -- ❌ rejected: 256 is not a Segment

Expressive pattern matching

Inspired by languages like Elixir, Gleam, Elm, and of course the functional programming diva Haskell, Extra supports pattern matching with variable assignment and exhaustiveness checking. Leave out a case and the compiler tells you which one.

enum PaymentStatus {
  .pending
  .paid
  .failed(reason: String)
}

fn describe(status: PaymentStatus, amount: Float): String =>
  switch status
  case .pending
    `⏳ Still waiting for $amount`
  case .paid
    `✅ $amount was paid`
  case .failed(reason:)
    `❌ $reason`

fn greet(name: String): String =>
  switch name
  case /(?<first>\w+) (?<last>\w+)/
    `Hello, $first $last!`
  case "Bob " .. last
    `Did you say Bab? Bab $last!?`
  else
    `Hello, $name!`

Lengths you can prove

Arrays, Strings, Dicts and Sets carry their lengths in their types. With L is Length, a signature can say how the lengths of its arguments and its result relate, and the compiler proves it: once where the function is written, and again at every call. An index that is proven valid gives you a B, not a B?. Nothing is checked at run time, and nothing is assumed.

fn zip<L is Length, A, B>(
  a: Array(A, length: L)
  b: Array(B, length: L)
): Array({A, B}, length: L) =>
  -- `index` is a valid index of `b`:
  -- b[index] is a B, not a B?
  a.map(|a-item, index| {a-item, b[index]})

fn pairs(names: Array(String), ages: Array(Int)) =>
  if names.length == ages.length then
    -- the lengths were compared:
    -- this call is proven safe
    zip(a: names, b: ages)
  else
    -- here zip(a: names, b: ages) is rejected:
    -- the length of 'ages' is not proven
    -- to be == names.length
    []