-
Notifications
You must be signed in to change notification settings - Fork 0
Guide String Interpolation
VelinScript unterstützt String Interpolation mit Format-Strings, ähnlich wie Python oder Rust.
String Interpolation verwendet geschweifte Klammern {} innerhalb von String-Literalen:
let name = "John";
let age = 30;
let message = "Hello, {name}! You are {age} years old.";
Die einfachste Form der Interpolation ist das Einfügen von Variablen:
let userName = "Alice";
let greeting = "Welcome, {userName}!";
// Ergebnis: "Welcome, Alice!"
Du kannst auch komplexere Ausdrücke innerhalb der geschweiften Klammern verwenden:
let x = 10;
let y = 20;
let result = "Sum: {x + y}";
// Ergebnis: "Sum: 30"
let price = 19.99;
let quantity = 3;
let total = "Total: {price * quantity}";
// Ergebnis: "Total: 59.97"
Funktionsaufrufe sind ebenfalls möglich:
fn getFullName(first: string, last: string): string {
return first + " " + last;
}
let firstName = "John";
let lastName = "Doe";
let fullName = "Name: {getFullName(firstName, lastName)}";
// Ergebnis: "Name: John Doe"
Methodenaufrufe auf Objekten:
let list = List<number>([1, 2, 3]);
let message = "List has {list.length()} items";
// Ergebnis: "List has 3 items"
Du kannst mehrere Interpolationen in einem String verwenden:
let product = "Laptop";
let price = 999.99;
let discount = 0.1;
let finalPrice = price * (1 - discount);
let message = "Product: {product}, Original: {price}, Discount: {discount * 100}%, Final: {finalPrice}";
// Ergebnis: "Product: Laptop, Original: 999.99, Discount: 10%, Final: 899.991"
Um geschweifte Klammern als Literale zu verwenden, musst du sie escapen:
// Escaping mit Backslash
let message = "This is a literal brace: \\{";
// Ergebnis: "This is a literal brace: {"
// Oder verwende einfache Anführungszeichen für Strings ohne Interpolation
let literal = 'This is {not interpolated}';
Format-Strings funktionieren auch mit Multi-line Strings:
let userId = "123";
let sql = "
SELECT * FROM users
WHERE id = {userId}
AND active = true
";
-
Verwende Format-Strings für bessere Lesbarkeit:
// Gut let message = "Hello, {name}!"; // Weniger gut let message = "Hello, " + name + "!"; -
Komplexe Ausdrücke in Variablen auslagern:
// Gut let total = price * quantity; let message = "Total: {total}"; // Auch OK, aber weniger lesbar let message = "Total: {price * quantity}"; -
Verwende Format-Strings für SQL-Queries:
let query = " SELECT * FROM products WHERE category = {category} AND price <= {maxPrice} ";
Format-Strings werden zu Rust format! Macros kompiliert:
// VelinScript
let message = "Hello, {name}!";
// Kompiliert zu:
let message = format!("Hello, {}!", name);Der Type Checker prüft, dass alle Ausdrücke innerhalb der geschweiften Klammern gültig sind und dass sie Typen haben, die als String formatiert werden können (implementieren Display Trait in Rust).
fn createResponse(userId: string, status: string): string {
return "{ \"userId\": \"{userId}\", \"status\": \"{status}\" }";
}
fn logInfo(component: string, message: string): void {
let logMessage = "[{component}] {message}";
// Log implementation
}
fn createError(operation: string, error: string): string {
return "Error in {operation}: {error}";
}
- Compiler Architecture
- Pass-Verlauf
- Type Inference
- Code Ordering
- IR Representation
- Borrow Checker
- Code Generation
- Multi-Target Compilation
- Module Resolution
- Framework Integration
- Parallelization
- AI Compiler Passes
- Prompt Optimizer
- System Generation
- Basics
- APIs
- Security
- Database
- Validation
- Authentication
- ML/LLM
- Intelligence Features
- Type Inference
- ML Training
- Pattern Matching
- Closures
- Collections
- HTTP Client
- String Interpolation
- Debugger
- Vektor-Datenbanken
- CLI Reference
- API Keys Setup
- Advanced
- Backend
- Security Best Practices
- AI/ML
- Auto Imports
- Plugin Development