Standard Library Overview¶
Raven comes with a comprehensive standard library that provides essential functionality for common programming tasks.
Available Modules¶
Core Modules¶
- Math - Mathematical operations and functions
- Collections - Data structures (maps, sets, lists)
- str - string manipulation and utilities (
import str from "str";) - Filesystem - File and directory operations
- Time - Date and time handling
- Network - HTTP and networking utilities
- JSON - JSON parsing, validation, minify/pretty utilities
- Testing - Unit testing framework
Built-in Functions¶
Raven provides several built-in functions that are always available:
Output Functions¶
// Print to console
print("Hello, World!");
// Formatted printing
print(format("User: {}, Age: {}", name, age));
Input Functions¶
// Get user input
let name: string = input("Enter your name: ");
Type Functions¶
// Get type information
let value: int = 42;
print(type(value)); // "int"
// Convert string to enum
let status: HttpStatus = enum_from_string("HttpStatus", "OK");
File Functions¶
// File operations
write_file("data.txt", "Hello, World!");
let content: string = read_file("data.txt");
let exists: bool = file_exists("data.txt");
Array Functions¶
// Array utilities
let numbers: int[] = [1, 2, 3, 4, 5];
print(len(numbers)); // 5
// Array methods
numbers.push(6);
let last: int = numbers.pop();
let slice: int[] = numbers.slice(1, 3);
Multi-dimensional arrays use repeated [] in the type and chained [index] in expressions (see Data types):
let grid: int[][] = [[1, 2], [3, 4]];
grid[0][1] = 9;
Using Modules¶
Import modules using the import statement:
import "math";
import "collections";
import str from "str";
import "json";
// Use functions from modules
let result: float = math_pow(2.0, 3.0);
let map: Map = new_map();
let formatted: string = string_format("Value: {}", 42);
let parsed: string[] = json.parse("{\"name\":\"Raven\"}");
JSON Module Quick Example¶
import "json";
let raw: string = "{\"name\":\"Raven\",\"stars\":27}";
// Validate JSON
let status: string = json.validate(raw); // "ok" on success
// Parse into path-indexed entries
let entries: string[] = json.parse(raw);
let name: string = json.get_value(entries, "$[name]");
// Formatting helpers
let compact: string = json.minify(raw);
let pretty: string = json.pretty(raw, 2);
Module Structure¶
Each module follows a consistent structure:
// Module: math.rv
export fun math_add(a: float, b: float) -> float {
return a + b;
}
export fun math_subtract(a: float, b: float) -> float {
return a - b;
}
// Structs can also be exported
export struct Vector {
x: float,
y: float,
z: float
}
Best Practices¶
- Import only what you need - Don't import unused modules
- Use descriptive names - Module functions are prefixed with module name
- Check documentation - Each module has detailed documentation
- Handle errors - Some functions may fail, check return values
- Use appropriate types - Match function parameter types exactly
Error Handling¶
Standard library functions use consistent error handling:
// File operations may fail
if (file_exists("config.txt")) {
let config: string = read_file("config.txt");
print(config);
} else {
print("Config file not found");
}
// Array operations check bounds
let numbers: int[] = [1, 2, 3];
if (len(numbers) > 0) {
let first: int = numbers[0];
print(first);
}
Performance Notes¶
- Built-in functions are optimized and fast
- Module functions may have slight overhead
- Array operations are efficient for most use cases
- string operations create new strings (immutable)
Future Modules¶
Planned standard library modules: - Regex - Regular expressions - Crypto - Cryptographic functions - Database - Database connectivity - Web - Web framework utilities
Next: Explore the GitHub repository for standard library source code