日本語

Types - Constraints

Difficulty: 🔴 Advanced
Time: 30 minutes

Basic Constraints

interface Comparable<T> {
    operator bool <(T other);
}

<T: Comparable> T min(T a, T b) {
    return a < b ? a : b;
}

AND Bounds

<T: Eq + Ord> T clamp(T value, T min_val, T max_val) {
    if (value < min_val) return min_val;
    if (value > max_val) return max_val;
    return value;
}

OR Bounds

<T: Numeric | Stringable> void display(T value) {
    println("{}", value);
}

where Clause

<T, U> Pair<T, U> make_pair(T first, U second)
where T: Clone, U: Clone {
    Pair<T, U> p;
    p.first = first.clone();
    p.second = second.clone();
    return p;
}

Built-in Interfaces

// Eq - Equality comparison
interface Eq<T> {
    operator bool ==(T other);
}

// Ord - Order comparison
interface Ord<T> {
    operator bool <(T other);
}

// Clone - Cloning
interface Clone {
    Self clone();
}

Previous: Interfaces
Next: match Expression

Last Updated: 2026-02-08