Files
sakurac/tests/programs/generic_bodies_rule_out_through_cases_1.sakura
T

39 lines
1.6 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
import std::io;
# Within a generic's own body, what follows a branch that leaves is checked
# once for each case its condition fails in, a trait ruled out rules out what
# implies it, as `numeric` does `integer`, and a body is checked in every case
# its condition holds in, up to 1024. A `match const` value is of the type its
# arms share there, in each instance.
type widened<T> = match const T { integer => i64, real => f64, else => T };
type kind_of<T> = match const T { integer => u8, else => u64 };
def which(x: u64): strview { return "u64"; }
def which(x: i64): strview { return "i64"; }
def widen<T>(x: T): widened<T> {
if const T is not integer && T is not real { return x; }
return match const T { integer => x as i64, else => x as f64 };
}
def kinded<T>(x: T): kind_of<T> {
if const T is not numeric { return 5u; }
return match const T { integer => 7 as u8, else => 9u };
}
def sums<A, B, C, D>(a: A, b: B, c: C, d: D): u64 {
if const (A is integer || A is real) && (B is integer || B is real) && (C is integer || C is real) && (D is integer || D is real) {
val w := a + a;
val x := b + b;
val y := c + c;
val z := d + d;
return 16;
}
return 0;
}
def typed<T>(x: T): strview { val n := match const T { integer => x as u64, real => 2u, else => 0 }; return which(n); }
def main(): void {
val w1: i64 = widen(3 as i8);
val w2: f64 = widen(1.5 as f32);
val w3: bool = widen(true);
println($"{w1} {w2} {w3} {kinded(true)} {kinded(3)} {kinded(2.5)} {sums(1, 2.5, 3 as u8, 4.5 as f32)} {sums(true, 1, 2, 3)}");
println($"{typed(3)} {typed(1.5)} {typed(true)}");
}