Wyn Examples
Learn Wyn through practical examples and tutorials.
Basic Examples
Hello World
fn main() {
print("Hello, World!")
}
Variables and Types
fn main() {
const name: str = "Wyn"
const version: int = 3
let mut counter = 0
print("Welcome to " + name + " v" + version.to_str())
counter += 1
print("Counter: " + counter.to_str())
}
Functions
fn add(a: int, b: int) -> int {
return a + b
}
fn greet(name: str) -> str {
return "Hello, " + name + "!"
}
fn main() {
const result = add(5, 3)
const message = greet("World")
print("5 + 3 = " + result.to_str())
print(message)
}
Advanced Examples
HTTP Server
import net
fn main() {
const server = net.server_create(8080)
print("Server running on http://localhost:8080")
while true {
const client = net.server_accept(server)
spawn handle_request(client)
}
}
fn handle_request(client: int) {
const request = net.server_read_request(client)
const path = net.parse_path(request)
match path {
"/" => {
const response = "Hello from Wyn server!"
net.server_send_response(client, 200, response)
}
"/api/status" => {
const json = "{\"status\": \"ok\", \"language\": \"wyn\"}"
net.server_send_json(client, 200, json)
}
_ => {
net.server_send_response(client, 404, "Not Found")
}
}
net.close(client)
}
Error Handling with Result Types
fn divide(a: int, b: int) -> Result[int, str] {
if b == 0 {
return err("Cannot divide by zero")
}
return ok(a / b)
}
fn parse_number(s: str) -> Result[int, str] {
// Simplified parsing example
if s == "42" {
return ok(42)
} else if s == "0" {
return ok(0)
} else {
return err("Invalid number: " + s)
}
}
fn main() {
// Basic error handling
const result1 = divide(10, 2)
match result1 {
ok(value) => print("10 / 2 = " + value.to_str())
err(msg) => print("Error: " + msg)
}
const result2 = divide(10, 0)
match result2 {
ok(value) => print("10 / 0 = " + value.to_str())
err(msg) => print("Error: " + msg)
}
// Chaining operations
const input = "42"
const chained = parse_number(input)
.and_then(|n| divide(n, 2))
.map(|n| n * 3)
match chained {
ok(value) => print("Chained result: " + value.to_str())
err(msg) => print("Chained error: " + msg)
}
}
Concurrency with Channels
import time
fn worker(id: int, ch: Channel[str]) {
for i in 0..3 {
const msg = "Worker " + id.to_str() + " - Task " + i.to_str()
print("Sending: " + msg)
ch.send(msg)
time.sleep_ms(100)
}
}
fn main() {
const ch: Channel[str] = channel_new()
// Spawn multiple workers
for worker_id in 0..3 {
spawn worker(worker_id, ch.clone())
}
// Collect results
const results: [str] = []
for _ in 0..9 { // 3 workers × 3 tasks each
match ch.recv() {
some(msg) => {
print("Received: " + msg)
results.push(msg)
}
none => break
}
}
print("Total messages received: " + results.len().to_str())
}
JSON Processing
import json
struct User {
name: str
age: int
email: str
}
fn main() {
// Parse JSON string
const json_str = "{\"name\": \"Alice\", \"age\": 30, \"email\": \"alice@example.com\"}"
match json.parse(json_str) {
ok(data) => {
const user = User{
name: data.get("name").as_str(),
age: data.get("age").as_int(),
email: data.get("email").as_str()
}
print("User: " + user.name)
print("Age: " + user.age.to_str())
print("Email: " + user.email)
// Serialize back to JSON
const user_json = json.object()
.put("name", user.name)
.put("age", user.age)
.put("email", user.email)
.to_string()
print("Serialized: " + user_json)
}
err(msg) => print("JSON parse error: " + msg)
}
}
More Examples
For more examples, check out the GitHub repository which contains:
- CLI Tools - Command-line applications
- Web Servers - HTTP servers and APIs
- Database Examples - PostgreSQL integration
- GUI Applications - Desktop applications
- Mobile Apps - iOS applications
- Algorithms - Data structures and algorithms
- Concurrency - Parallel processing examples