Examples

See the languages at work.

These examples are selected from executable fixtures. Dewy and µDewy are related but distinct source languages, so every example is labeled.

Dewy · runs

Hello, World

printl"Hello, World!"

Prelude imports and top-level execution in one line.

Dewy · runs

Array sum

let values:array<int64> = [10 20 12]
let i:int64 = 0
let total:int64 = 0
loop i <? values.length {
    total += values[i]
    i += 1
}

Flow-proven bounds make the indexed access valid.

Dewy · runs

Objects

let point = [
    x = 3
    y = 4
    sum = () => x + y
]
let value = point.sum

Structural fields and methods with object-local lookup.

Dewy · runs

Unicode strings

let text = "café 👨‍👩‍👧‍👦"
let family = text[5]
let count = family.length

Index and length operate on extended grapheme clusters.

Dewy · runs

Stepped ranges

let total:int64 = 0
loop i in 0,2..10 {
    total += i
}
loop i in 5,3..0 {
    total += i
}

A second anchor establishes either a positive or negative step without a separate range constructor.

Dewy · runs

Multiple iterators

let total:int64 = 0
loop i in 0..2 and j in 10..20 {
    total += i
}

Iterator conditions compose directly with logical operators; and stops when either iterator is exhausted.

Dewy · runs

Optional narrowing

let value:int64|none = choose(true)

if value is? int64 {
    return value + 2
}
return 0

A type test narrows the optional value for the successful branch without an assertion or forced unwrap.

Dewy · runs

Overload sets

let choose = (():>int64 => 20)
           & ((x:int64):>int64 => x)

let answer = choose() + choose(22)

& combines ordinary function values, and each call selects the implementation matching its arguments.

Dewy · runs

Defaults and keywords

let add = (x:int64 y:int64=2):>int64 =>
    x + y

let answer = add(40)
let same = add(39 y=3)

Parameters can have lazy defaults, while named arguments make overrides and reordered calls explicit.

Dewy · runs

Modules and paths

from p"lib.dewy" import (answer add Number)
import p"lib.dewy" as library

let value:library.Number = answer
let result = add(value)

Selective and namespaced imports share file-relative path values and one type system across modules.

µDewy · playground

Fibonacci

let fib = (n:int):>int => {
    if n <=? 1 { return n }
    return fib(n - 1) + fib(n - 2)
}

Compile the bootstrap subset entirely in the browser.

µDewy · runs

Self-hosting

udewy -c \
  udewy/bootstrap/main.udewy

The µDewy compiler is itself implemented in µDewy.

More programs

The repository also contains backend tests, web and SDL demos, games, data structures, graphics experiments, and older design examples. Some files under the top-level examples/ directory describe planned language behavior rather than the current compiler.