The Language Tour
This tour introduces the key concepts and syntax of Kyna through short, focused examples. Each section covers a specific language feature with explanations and outputs.
1. Variables & Bindings
In Kyna, use var for mutable variables, const for immutable constants, and fn to declare functions. Types can be written explicitly or inferred by the compiler:
# Declare a function with typed parameters and return type
fn double(value: int): int {
return value * 2;
}
var city: str = "San Francisco";
const answer = double(21); # Inferred as int (42)
print("City:", city);
print("Answer:", answer);
Output:
City: San Francisco
Answer: 42
2. Conditionals (if / else if / else)
Condition expressions in Kyna are always enclosed in parentheses, and the block body must be enclosed in braces:
fn grade(score: int): str {
if (score >= 90) {
return "A";
} else if (score >= 80) {
return "B";
} else if (score >= 70) {
return "C";
} else {
return "F";
}
}
print("Score 85:", grade(85));
print("Score 62:", grade(62));
Output:
Score 85: B
Score 62: F
3. Pattern Matching with switch
Kyna's switch statements evaluate arms independently without implicit fall-through. Each arm requires braces:
fn describeStatus(code: int): str {
switch (code) {
case 200: {
return "OK";
}
case 404: {
return "Not Found";
}
case 500: {
return "Internal Server Error";
}
default: {
return "Unknown Status";
}
}
}
print("Status 200:", describeStatus(200));
print("Status 404:", describeStatus(404));
print("Status 418:", describeStatus(418));
Output:
Status 200: OK
Status 404: Not Found
Status 418: Unknown Status
4. Loops (while and loop)
Kyna provides standard while loops as well as a three-clause loop construct for structured iteration:
# Standard while loop
var count = 0;
while (count < 3) {
print("While count:", count);
count = count + 1;
}
# Structured three-clause loop: (init; condition; increment)
var sum = 0;
loop (var i = 1; i <= 3; i = i + 1) {
sum = sum + i;
}
print("Sum 1..3:", sum);
Output:
While count: 0
While count: 1
While count: 2
Sum 1..3: 6
5. Classes & Object-Oriented Programming
Define classes with constructors (init), member fields, and instance methods:
class Task {
public init(title: str, priority: int) {
self.title = title;
self.priority = priority;
self.completed = false;
}
fn complete() {
self.completed = true;
}
fn describe(): str {
var status = "pending";
if (self.completed) {
status = "done";
}
return self.title + " (" + status + ")";
}
}
const task = Task("Write Documentation", 1);
task.complete();
print("Task status:", task.describe());
Output:
Task status: Write Documentation (done)
6. Null Safety & Fallible Values
Kyna distinguishes between non-nullable types (str) and nullable types (str?). When a function might return null, declare it explicitly:
fn findPort(service: str): int? {
if (service == "http") {
return 80;
} else if (service == "https") {
return 443;
}
return null; # Explicit null
}
var port = findPort("https");
if (port != null) {
print("Port is:", port);
} else {
print("Unknown service port");
}
Output:
Port is: 443
7. Asynchronous Operations (async / await)
Asynchronous functions return promises and can be awaited without blocking the execution thread:
async fn fetchServerTime(): str {
try {
const response = await fetch("https://api.kyna.dev/time");
return response;
} catch (err) {
return "Fallback time";
}
}
const time = await fetchServerTime();
print("Server time:", time);
8. Modules & packages
export publishes a name. import "./math.kyna" as math loads that file and exposes only exports. A directory passed to ky check / ky run is one package — sibling files share a namespace. Packages under internal/ cannot be imported from outside their parent tree.
import "./math.kyna" as math;
var total: int = math.add(20, 22);
See Modules & Packages for named/default imports, .kyc export cache, and KSEM1042.
Key Takeaways
- Explicit Signatures: Function parameters and return values carry clear type annotations.
- No Ambiguous Syntax: Braces are required for block bodies;
switcharms never fall through accidentally. - Strict Null Safety: Nullable values are explicit (
T?) and type-narrowed safely. - Inspectable Runtime: Every stage of code compilation can be viewed using the
kyCLI.
Explore the deep dives in the Tutorial and Reference sections to learn more.