NATIVE FOUNDATIONS
Native foundations
Pass an input to a small function and add 42. The program keeps the syntax easy to follow.
Read the guide# Add a fixed amount to the input.
func entry(value: i32) -> i32:
return value + 42LET THE CODE SPEAK
Choose a topic to explore complete VOID source, its expected result and the idea behind it. These are source examples; public compiler builds are not yet available.
NATIVE FOUNDATIONS
Pass an input to a small function and add 42. The program keeps the syntax easy to follow.
Read the guide# Add a fixed amount to the input.
func entry(value: i32) -> i32:
return value + 42FUNCTIONS & RECURSION
Calculate a factorial with one recursive function and a clear stopping point.
Read the guide# Factorial is defined here for non-negative inputs.
func factorial(number: i32) -> i32:
if number <= 1:
return 1
return number * factorial(number - 1)
func entry(number: i32) -> i32:
return factorial(number)READABLE LOOPS
Add the odd numbers from one through the limit using a single conditional loop.
Read the guide# Add the odd numbers from 1 through the limit.
func entry(limit: i32) -> i32:
var total = 0
var number = 1
for number <= limit:
total += number
number += 2
return totalGROWABLE LISTS
Append a new score to a list, then add the scores together.
Read the guidefunc entry(latest_score: i32) -> i32:
var scores: [i32] = [10, 20]
scores.push(latest_score)
var total = 0
for score in scores:
total += score
return totalVALUES & SHARED OBJECTS
Copy a point independently while two names share one counter.
Read the guidevalue struct point:
x: i32
y: i32
struct counter:
total: i32
func entry(value: i32) -> i32:
var original = point(x = 2, y = 3)
var copied = original
copied.x += 10
var shared = counter(total = 0)
var alias = shared
alias.total += 5
return original.x + copied.x + shared.totalRESULTS & MATCHING
Return a recoverable result and handle each case explicitly.
Read the guidefunc divide(value: i32) -> result<i32, string>:
if value == 0:
return Err("division by zero")
return Ok(100 / value)
func entry(value: i32) -> i32:
var outcome = divide(value)
return match outcome:
Ok(var number): number
Err(var reason): -1func larger<t>(first: t, second: t) -> t:
if first > second:
return first
return second
func entry(score: i32) -> i32:
return larger(score, 10)value struct rectangle:
width: i32
height: i32
property (shape rectangle) area: i32:
get:
return shape.width * shape.height
func entry(width: i32) -> i32:
var panel = rectangle(width = width, height = 3)
return panel.areafunc counter(start: i32) -> func() -> i32:
var number = start
return func() -> i32:
number += 1
return number
func entry(value: i32) -> i32:
var next = counter(value)
var alias = next
var first = next()
return first + alias()struct sensor:
event measured: func(i32)
func (source sensor) publish(reading: i32):
source.measured.emit(reading)
struct display:
latest_reading: i32 = 0
func (screen display) update(reading: i32):
screen.latest_reading = reading
func entry(reading: i32) -> i32:
var source = sensor()
var screen = display()
source.measured += screen.update
source.publish(reading)
return screen.latest_readingSCOPED CLEANUP
Use a scope to own cleanup of a small in-memory workspace.
An in-memory teaching model of scope ownership; no external file or native-resource API is assumed.
Read the guide# An in-memory model of a workspace with scope-owned cleanup.
struct temporary_workspace:
item_count: i32
func (workspace temporary_workspace) deinit():
workspace.item_count = 0
func entry(item_count: i32) -> i32:
var processed = 0
with temporary_workspace(item_count = item_count) as workspace:
processed = workspace.item_count * 2
return processedASYNC & AWAIT
Start work now. Await its result when you need it.
Requires the explicit async host adapter; the ordinary synchronous CLI entry adapter does not execute this program.
Read the guideasync func leaf(value: i32) -> i32:
return value + 1
async func entry(value: i32) -> i32:
var first = leaf(value)
var second = leaf(value + 1)
var a = await first
var b = await second
return a + bTASK GROUPS
Schedule two updates and let the task group wait for both before reading the total.
Requires the explicit async host adapter; the ordinary synchronous CLI entry adapter does not execute this program.
Read the guidestruct progress:
completed: i32 = 0
async func record_work(report: progress, amount: i32):
report.completed += amount
async func entry(amount: i32) -> i32:
var report = progress()
with tasks.group() as jobs:
var first = record_work(report, amount)
var second = record_work(report, amount + 1)
return report.completedPARALLEL WORKERS
Compute two squares independently on native workers and add their results.
Requires the explicit async host adapter; the ordinary synchronous CLI entry adapter does not execute this program.
Read the guidefunc square(number: i32) -> i32:
return number * number
async func entry(number: i32) -> i32:
var first = spawn square(number)
var second = spawn square(number + 1)
return await first + await secondMODULES & IMPORTS
Organize a program into small source files.
This example also needs math/calculator.void and helpers/numbers.void, kept in those relative paths.
Read the guideuse "/math/calculator.void" as calculator
use "/helpers/numbers.void"
func entry(value: i32) -> i32:
return calculator.double(value) + increment(value)