Sn Tin

Coming from Python or TypeScript

The same idea, three ways. The Tin column is compiled every time these docs are built. The last three rows are where Tin stops looking like the others: tools, typed replies and approval are part of the language, not a library.

PythonTypeScriptTin

Variables

name = "Ada"
age = 36
const name = "Ada"
let age = 36
let name = "Ada"
var age = 36

String formatting

f"{name} is {age}"
`${name} is ${age}`
"{name} is {age}"

Functions

def area(w: float, h: float) -> float:
    return w * h
function area(w: number, h: number): number {
  return w * h
}
fn area(w: float, h: float): float => w * h

Lambdas and map

[x * x for x in xs]
xs.map(x => x * x)
xs.map(x => x * x)

Pipelines

sum(map(f, xs))
xs.map(f).reduce((a, b) => a + b, 0)
xs |> map(f) |> sum

Missing values

d.get("bob")  # None
map.get("bob")  // undefined
ages.get("bob")  // None, an Option<int>
ages.get("bob").or(0)

Errors

raise ValueError("bad port")
throw new Error("bad port")
return Err("bad port")   // a Result<T, string>
let port = parsePort(text)?

Records

@dataclass
class Point:
    x: float
    y: float
type Point = { x: number; y: number }
type Point = { x: float, y: float }

Enums with data

Shape = Circle | Rect  # by convention
type Shape =
  | { kind: "circle"; r: number }
  | { kind: "rect"; w: number; h: number }
type Shape =
  | Circle { r: float }
  | Rect { w: float, h: float }

Concurrency

task = asyncio.create_task(brew())
await task
const task = brew()
await task
let task = spawn brew()   // brew is an ordinary fn
await task

A tool for a model

@tool
def add(a: int, b: int) -> int:
    """Add two integers."""
    return a + b
tool({ name: "add", description: "Add two integers.",
  parameters: z.object({ a: z.number(), b: z.number() }) })
/// Add two integers.
tool fn add(a: int, b: int): int => a + b

Structured output

Order.model_validate_json(reply)
OrderSchema.parse(JSON.parse(reply))
let order: Order = Clerk("two oat lattes")

Human approval

if input("ok? ") == "y": wire()
if (await confirm("ok?")) wire()
guarded tool fn wire(to: string, amount: int): string => "sent"

Next: the tour, or try any of it in the playground.