Odin
2016fragletMCP + fragletc
Welcome to the Odin implementation of Hello World!
About Odin
Odin is a modern systems programming language designed to be a "joy to use" alternative to C and C++. Built with simplicity, performance, and clarity in mind! ๐
What makes Odin special?
- Simplicity over complexity: Clean, readable syntax that doesn't get in your way
- Fast compilation: Lightning-fast build times that keep you in the flow
- Zero hidden allocations: Memory management is explicit and under your control
- Built-in data-oriented design: First-class support for arrays, slices, and data structures
- Seamless C interop: Easy integration with existing C libraries and code
Fun Facts
- ๐ฎ Game Development: Odin was originally created for game development and excels at real-time applications
- โก Performance: Compiles to fast native code via LLVM backend
- ๐งฉ Data-oriented: Built from the ground up with data-oriented programming principles
- ๐ฏ Explicit: No hidden control flow, allocations, or mysterious magic
- ๐ ๏ธ Pragmatic: Focuses on getting things done without unnecessary ceremony
This Implementation
This Hello World program showcases Odin's clean and explicit syntax. The fmt.println("Hello World!") call demonstrates how Odin keeps things simple and readable while maintaining the power of a systems language.
Language: Odin Paradigm: Systems Programming Fun Level: Maximum productivity! ๐ฏ
Hello World
package main
// BEGIN_FRAGLET
import "core:fmt"
main :: proc() {
fmt.println("Hello World!")
}
// END_FRAGLETCoding Guide
Language Version
Odin (latest from source, built with LLVM)
Execution Model
- Compiled language using
odin build - Code is compiled to a binary, then executed
- Standard Odin execution model with
main :: proc()procedure inmainpackage
Key Characteristics
- Statically typed with type inference
- Case-sensitive
- Explicit memory management (no garbage collection by default)
- Procedure-based (functions are called "procedures")
- Package-based module system
- Built-in support for arrays, slices, and dynamic arrays
- Strong C interop capabilities
- Data-oriented design principles
- Zero hidden allocations
Fragment Authoring
The package main declaration is pre-provided. Your fragment includes import statements and the main :: proc() procedure definition. You can also define additional procedures and types.
Available Packages
The template includes the standard library package:
core:fmt- Formatted I/O (println, printf, printfln)
Additional standard library packages can be imported as needed:
core:strings- String manipulationcore:strconv- String conversionscore:os- Operating system interfacecore:io- I/O primitivescore:math- Mathematical functionscore:time- Time operationscore:mem- Memory managementcore:slice- Slice operationscore:sort- Sorting algorithms- And many more from the Odin standard library
Common Patterns
- Print:
fmt.println("message")orfmt.printf("format %s\n", value) - Variables:
x: int = 10orx := 10(type inference) - Procedures:
add :: proc(a, b: int) -> int { return a + b } - Structs:
Person :: struct { name: string; age: int } - Arrays:
numbers: [5]int = {1, 2, 3, 4, 5} - Slices:
numbers := []int{1, 2, 3}ornumbers := make([]int, 0, 10) - Dynamic arrays:
numbers: [dynamic]int - Maps:
m := make(map[string]int)orm: map[string]int = {"a" = 1} - For loops:
for i in 0..<len(arr) { ... }orfor value in arr { ... } - If statements:
if condition { ... } else { ... }
Examples
// Simple output
main :: proc() {
fmt.println("Hello from fragment!")
}
// Variables and calculations
main :: proc() {
a: int = 5
b: int = 10
fmt.printf("Sum: %d\n", a + b)
}
// Procedures
add :: proc(a, b: int) -> int {
return a + b
}
main :: proc() {
result := add(5, 10)
fmt.printf("5 + 10 = %d\n", result)
}
// Arrays and loops
main :: proc() {
numbers := []int{1, 2, 3, 4, 5}
sum := 0
for num in numbers {
sum += num
}
fmt.printf("Sum: %d\n", sum)
}
// Structs
Person :: struct {
name: string,
age: int,
}
main :: proc() {
p := Person{name = "Alice", age = 30}
fmt.printf("%s is %d years old\n", p.name, p.age)
}
// Maps
main :: proc() {
m := make(map[string]int)
m["apple"] = 5
m["banana"] = 3
fmt.printf("Apples: %d\n", m["apple"])
}
// String operations
main :: proc() {
s := "Hello"
s2 := " World!"
fmt.println(s)
fmt.println(s2)
fmt.printf("First length: %d\n", len(s))
fmt.printf("Second length: %d\n", len(s2))
}
// Type inference and multiple return values
divide :: proc(a, b: int) -> (int, bool) {
if b == 0 {
return 0, false
}
return a / b, true
}
main :: proc() {
result, ok := divide(10, 2)
if ok {
fmt.printf("Result: %d\n", result)
} else {
fmt.println("Division by zero!")
}
}Caveats
- Fragments must be valid Odin code that compiles
- Odin uses
::for procedure and type definitions (not:) - Variables can use type inference with
:=or explicit typing with: - Arrays are value types, slices are reference types
- Maps are reference types
- Zero values:
0for numbers,""for strings,nilfor pointers/slices/maps - String concatenation uses
strings.concatenate()or the+operator (depending on context) - Procedure syntax:
name :: proc(params) -> return_type { ... } - Import statements use
import "package:name"syntax - Odin is case-sensitive
- Memory management is explicit - no automatic garbage collection by default
Fraglet Scripts
Echo Args
#!/usr/bin/env -S fragletc --vein=odin
import "core:fmt"
import "core:os"
import "core:strings"
main :: proc() {
args := os.args[1:]
fmt.println("Args:", strings.join(args, " "))
}Stdin Upper
#!/usr/bin/env -S fragletc --vein=odin
import "core:fmt"
import "core:os"
import "core:strings"
main :: proc() {
buf: [4096]u8
for {
n, err := os.read(os.stdin, buf[:])
if err != nil || n == 0 { break }
line := string(buf[:n])
fmt.print(strings.to_upper(line))
}
}Test
#!/usr/bin/env -S fragletc --vein=odin
import "core:fmt"
main :: proc() {
fmt.println("Hello World!")
}