Introduction
What Extra is, and a first look at its syntax.
Extra is a strongly-typed language and runtime for building client-side applications. Its closest cousin is Elm, it's a second cousin to React, a long-time-listener-first-time-caller to Svelte, and it has an uncanny-valley similarity to TypeScript.
Why another language?
Elm made good on the promise of being extremely well-reasoned, but composing components that need
their own internal state was painful. Extra makes that easy, and still explicit. While I was in
there, I added TypeScript's branch-based type narrowing and derived types, Swift's guard, and JSX
(made a bit more ergonomic).
Extra is also aimed at being a verifiable language. The type checker proves things that other languages leave to tests or to run-time checks: that an index is in range, that two arrays have the same length, that a number is within the range a type allows. When it cannot prove something, it rejects the program and says what it could not prove. It never assumes.
Spreadsheets, not diffing
The big difference from "render and diff" frameworks is how views are updated. When you change a cell in a spreadsheet, the spreadsheet knows exactly which cells depend on it, and updates only those. Extra works the same way: a view records which state it depends on, and a change (assign a new string, push to an array) is handed to exactly the views that use it. There is no diffing.
A first look
-- comments are hyphenated, like Ada
{- or nested, like Haskell and Elm -}
-- the type checker infers the result:
-- [2 | 4 | 6 | 8 | 10, length: =5] | [2 | … | 20, length: =10]
fn evens(max: Int) =>
if max <= 10 then
[2, 4, 6, 8, 10]
else
[2, 4, 6, 8, 10] ++ [12, 14, 16, 18, 20]
-- the pipe operator puts the left side in `#pipe`
fn odd-labels(numbers: Array(Int)) =>
numbers.filter(|n| n % 2 == 1)
|> #pipe.map(|n| `#$n`)
-- `onlyif` includes an item only when its condition holds
-- result: [1 | 3 | 5 | 7 | 9 | 11, length: 5...6]
fn odds(include-eleven: Boolean) =>
[
1
3
5
...[7, 9]
11 onlyif include-eleven
]
-- `?|>` skips the pipe when the value is null
fn label(# name: String?) =>
name ?|> #pipe .. "!"Views and messages
A view holds state (the names that start with @) and renders. 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 message with arguments is a constructor: onChange=set-name. Because
every change is a message, a debugger can record each one, with the state before and after.
This counter runs in a terminal, with the TeaUI provider:
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>
}Next
- Length generics: signatures that relate the lengths of their arguments and results, proven by the compiler.