Save a card using its shown .prnc or .princi filename, then build it with the Windows x86-64 compiler:
princi build hello.prnc.prnc and .princi are interchangeable aliases for the same source language.Each output panel is static expected output; the website does not run Princi code.20 examples in the v0.2 gallery
Hello World
hello.prnc
- main
- String
- println
fn main() {
println("Hello from PrinciPL!")
}Every executable starts at a zero-argument main function. println writes a String followed by a newline.
Hello from PrinciPL!
Variables
variables.princi
- let
- var
- type inference
- compound assignment
fn main() {
let label: String = "items"
var count = 2
count += 1
println(label)
println(count)
}let binds an immutable value, while var allows reassignment. The compiler infers count as Int from its initializer.
items 3
Functions
functions.prnc
- parameters
- return types
- function calls
fn add(left: Int, right: Int) -> Int {
return left + right
}
fn main() {
println(add(20, 22))
}Parameters and return values have declared types. A function without an explicit return type returns Void.
42
Factorial / Recursion
factorial.princi
- recursion
- if
- return
- Int
fn factorial(n: Int) -> Int {
if n <= 1 {
return 1
}
return n * factorial(n - 1)
}
fn main() {
println(factorial(5))
}A function can call itself. This example returns 1 for the base case, then multiplies by the result for the previous integer.
120
Conditions
conditions.prnc
- if
- else
- Bool
- comparison
fn main() {
let score = 72
if score >= 60 && score < 80 {
println("pass")
} else {
println("keep practicing")
}
}if and else choose a block from a Bool condition. Comparisons produce Bool, and && combines two Boolean expressions.
pass
While Loop
countdown.princi
- while
- mutable local
- comparison
fn main() {
var count = 0
while count < 3 {
count += 1
}
println(count)
}A while loop repeats as long as its Bool condition is true. The loop changes count on each pass.
3
For Range Loop
sum-range.prnc
- for
- integer range
- end-exclusive bounds
fn main() {
var total = 0
for value in 1..5 {
total += value
}
println(total)
}The range 1..5 includes 1, 2, 3, and 4. Its end bound is exclusive, so this loop prints their sum.
10
Strings
greeting.princi
- String
- concatenation
- println
fn main() {
let greeting = "Hello, " + "PrinciPL!"
println(greeting)
}The + operator concatenates two strings and creates a new String value. String interpolation is not part of v0.2.
Hello, PrinciPL!
Classes
user.prnc
- class
- reference value
- field access
class User {
name: String
age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
}
fn main() {
var user = User("Ada", 20)
println(user.name)
}A class groups typed instance fields. Class values are managed references, and fields are accessible under the v0.2 public-by-default rule.
Ada
Constructor
message.princi
- init
- self
- object construction
class Message {
text: String
init(text: String) {
self.text = text
}
}
fn main() {
let message = Message("Ready")
println(message.text)
}A class may declare one init constructor. Its parameters are passed to positional construction and can initialize fields through self.
Ready
Methods
counter.prnc
- instance methods
- self
- static dispatch
class Counter {
value: Int
init(value: Int) {
self.value = value
}
fn increment() {
self.value += 1
}
fn current() -> Int {
return self.value
}
}
fn main() {
var counter = Counter(1)
counter.increment()
println(counter.current())
}Instance methods use self to access the receiver. Concrete class calls use static dispatch; interface and trait values use dynamic dispatch.
2
Structs
point.princi
- struct
- value semantics
- typed fields
struct Point {
x: Float
y: Float
}
fn distanceSquared(point: Point) -> Float {
return point.x * point.x + point.y * point.y
}
fn main() {
let point = Point(3.0, 4.0)
println(distanceSquared(point))
}Structs store fields inline and copy by value when passed or returned. This function reads a Point without changing it.
25
List<T>
numbers.prnc
- List<Int>
- indexing
- add
- length
fn main() {
var numbers: List<Int> = [1, 2, 3]
numbers[1] = 7
numbers.add(9)
println(numbers.length)
println(numbers[1])
}List<T> checks all values against one element type. Index reads and writes have runtime bounds checks; add appends an item.
4 7
Imports
modules.princi
- import io
- import math
- built-in modules
import io
import math
fn main() {
println("Built-in imports resolved")
}Only the built-in io and math module names resolve in v0.2. Imports do not create namespaces, and math has no exported functions yet.
Built-in imports resolved
Limited C FFI
abs.prnc
- extern "C"
- Int32
- Windows x86-64 ABI
extern "C" {
fn abs(value: Int32) -> Int32
}
fn main() {
println(abs(-42))
}This declares a direct C ABI function using a supported primitive signature. The Windows C runtime supplies abs; managed values and pointers cannot cross this boundary.
42
Generic Functions
identity.prnc
- type parameters
- inference
- monomorphization
fn identity<T>(value: T) -> T {
return value
}
fn main() {
println(identity(42))
println(identity<String>("hello"))
}The first call infers T as Int; the second specifies String explicitly. The compiler emits only the concrete specializations this program uses.
42 hello
Generic Class and Struct
pair.princi
- Box<T>
- Pair<A, B>
- class references
- struct values
class Box<T> {
value: T
init(value: T) {
self.value = value
}
fn get() -> T {
return self.value
}
}
struct Pair<A, B> {
first: A
second: B
}
fn main() {
let box = Box<Int>(42)
let pair = Pair<Int, String>(box.get(), "one")
println(pair.first)
println(pair.second)
}Generic classes specialize reference layouts, while generic structs specialize inline value layouts. Type arguments are explicit here.
42 one
Generic Enum and Match
maybe.prnc
- enum
- payload
- pattern matching
- exhaustiveness
enum Maybe<T> {
Some(T)
None
}
fn main() {
let item = Maybe<Int>.Some(42)
match item {
Some(value) => println(value)
None => println("empty")
}
}The Some arm binds its payload in an arm-local immutable variable. The match covers both variants, so it is exhaustive.
42
Interfaces and Traits
named.princi
- interface dispatch
- trait default
- trait value
interface Identifiable {
fn id() -> Int
}
trait Named {
fn name() -> String
fn describe() {
println(self.name())
}
}
class User implements Identifiable, Named {
value: String
init(value: String) {
self.value = value
}
fn id() -> Int { return 7 }
fn name() -> String { return self.value }
}
fn main() {
let user: Identifiable = User("Ada")
println(user.id())
let named: Named = User("Grace")
named.describe()
}The interface supplies a pure contract; the trait supplies a default method. Interface and trait values dispatch through the concrete class implementation.
7 Grace
Result and ?
result.prnc
- Result<T, E>
- Ok
- Err
- ? propagation
fn calculate(value: Int) -> Result<Int, String> {
if value < 0 {
return Result<Int, String>.Err("must be positive")
}
return Result<Int, String>.Ok(value * 2)
}
fn run() -> Result<Int, String> {
let doubled = calculate(21)?
return Result<Int, String>.Ok(doubled)
}
fn main() {
match run() {
Ok(value) => println(value)
Err(error) => println(error)
}
}The postfix ? returns an Err from run with the same error type, or yields the successful Int value. Match handles both Result variants.
42