Alpha. Vary is under active development and not ready for production use. Syntax, APIs, performance, and behaviour may change between releases.
Syntax overview
A guided tour of the Vary language, organized by topic. Each page is self-contained and links to detailed reference pages.
# Variables and types
let name = "Vary"
let version: Float = 0.1
mut count = 0
# Optionals
let maybe: Str? = None
if maybe is not None {
print(maybe) # narrowed to Str
}
# String interpolation
print("Hello from ${name} v${version}")def greet(name: Str, loud: Bool = False) -> Str {
if loud {
return "HELLO, ${name.upper()}!"
}
return "Hello, ${name}"
}
# Lambdas
let double = (x: Int) -> Int : x * 2
# Contracts
def withdraw(balance: Int, amount: Int) -> Int {
in { assert amount > 0 }
out result { assert result >= 0 }
return balance - amount
}# Classes with constructors
class Person(name: Str, age: Int) {
def greeting() -> Str {
return "I'm ${name}, age ${age}"
}
}
# Data types (auto equals, hashCode, toString)
data Point { x: Int; y: Int }
# Enums with payloads
enum Shape {
Circle(radius: Float)
Rect(w: Float, h: Float)
}
# Interfaces
interface Drawable {
def draw() -> Str
}# Lists
let nums = [1, 2, 3, 4, 5]
let evens = nums.filter((n: Int) -> Bool : n % 2 == 0)
# Dicts
let scores: Dict[Str, Int] = {
"Alice": 95,
"Bob": 87
}
# Comprehensions
let squares = [x * x for x in range(10) if x % 2 == 0]
# Iteration
for name in scores.keys() {
print("${name}: ${scores[name]}")
}enum Expr {
Num(val: Int)
Add(left: Expr, right: Expr)
Neg(inner: Expr)
}
def eval(e: Expr) -> Int {
match e {
case Num(val) { return val }
case Add(left, right) {
return eval(left) + eval(right)
}
case Neg(inner) {
return -eval(inner)
}
}
}
let expr = Add(Num(1), Neg(Num(2)))
print(eval(expr)) # -1def fibonacci(n: Int) -> Int {
if n <= 1 { return n }
return fibonacci(n - 1) + fibonacci(n - 2)
}
test "fibonacci base cases" {
expect_eq(fibonacci(0), 0)
expect_eq(fibonacci(1), 1)
}
test "fibonacci recursive" {
expect_eq(fibonacci(10), 55)
}
test "fibonacci is always non-negative" {
for i in range(15) {
expect_true(fibonacci(i) >= 0)
}
}| Page | Covers |
|---|---|
| Basics | Comments, variables, types, optionals, strings |
| Collections | Lists, dicts, sets, comprehensions |
| Functions | Functions, lambdas, operators |
| Control flow | If/elif/else, while, for, match/case |
| Types and data | Classes, data types, interfaces, enums, generics |
| Error handling | Contracts, try/except, Result type, defer |
| Modules and more | Imports, concurrency, testing, embedded DSLs |