Skip to content

Variables & Types

Variables

wyn
var x = 42          // mutable
const y = 100       // immutable

Tuple Unpacking

Declare several variables at once from a tuple literal or a tuple-returning function:

wyn
var a, b = 10, 20              // a = 10, b = 20
wyn
fn divmod(a: int, b: int) -> (int, int) {
    return (a / b, a % b)
}

var q, r = divmod(17, 5)       // q = 3, r = 2

Multi-Assignment and Swap

Assign to several existing variables in one statement. The right-hand side is fully evaluated before anything is written, so swaps and rotations just work — no temporary needed:

wyn
var a = 1
var b = 2
a, b = b, a                    // swap → a = 2, b = 1

var x = 1
var y = 2
var z = 3
x, y, z = z, x, y              // rotate → x = 3, y = 1, z = 2

Basic Types

wyn
var age: int = 25           // 64-bit integer
var price: float = 19.99    // double precision
var name: string = "Alice"  // string
var active: bool = true     // boolean

Type inference works — you can omit the type when the compiler can figure it out:

wyn
var count = 0       // int
var pi = 3.14       // float
var msg = "hello"   // string

Strings

String interpolation with ${}:

wyn
var name = "Wyn"
var version = 1
println("${name} v${version}")   // "Wyn v1"

Arrays

wyn
var numbers = [1, 2, 3, 4, 5]
var names: [string] = ["Alice", "Bob"]

var first = numbers[0]          // 1
var len = numbers.len()         // 5

Negative Indexing

Index from the end with negative numbers — a[-1] is the last element:

wyn
var nums = [10, 20, 30, 40]
println(nums[-1].to_string())   // 40 — last
println(nums[-2].to_string())   // 30 — second to last

List Comprehensions

wyn
var squares = [x * x for x in 0..10]
var evens = [x for x in 0..20 if x % 2 == 0]

Slices

Slice with a[i:j]. Either bound can be omitted — a[:j] starts at the beginning, a[i:] runs to the end, and a[:] copies the whole array:

wyn
var nums = [1, 2, 3, 4, 5]
var first3 = nums[0:3]             // [1, 2, 3]
var head   = nums[:2]              // [1, 2]      — first two
var tail    = nums[2:]             // [3, 4, 5]   — from index 2
var copy    = nums[:]              // [1, 2, 3, 4, 5]

var hello  = "hello world"[0:5]    // "hello"
var world  = "hello world"[6:]     // "world"

Constants

wyn
const MAX_SIZE = 1024
const PI = 3.14159
const GREETING = "Hello"

Try It

🐉 Playground
Press Run or Ctrl+Enter

See Also

MIT License — v1.16