25 lines
1.2 KiB
Plaintext
25 lines
1.2 KiB
Plaintext
# A ternary choosing while compiling checks each branch with what its
|
|||
|
|
# condition says, and a note said in one case says which. A `match const` of
|
||
|
|
# values chooses by a constant, its patterns of the constant's type, and an
|
||
|
|
# instance none fits, without `else`, is an error. Each instance checks the
|
||
|
|
# branch it takes, so `&` of what a `*T` points at writes nothing.
|
||
|
|
type addref<T, const M: bool> = M ? &mut T : &T;
|
||
|
|
type castref<T> = match const T {
|
||
|
|
reference => T,
|
||
|
|
pointer => addref<T::target, T::mutable>,
|
||
|
|
else => addref<T, false>
|
||
|
|
};
|
||
|
|
|
||
|
|
pub def tern<T>(ptr: T): castref<T> where T: pointer { return T::mutable ? &mut ptr[0] : 5; }
|
||
|
|
def pats<const N: u64>(): u8 { return match const N { 4 => 1, "a" => 2, else => 3 }; }
|
||
|
|
def typed<T>(): u8 { return match const T { 4 => 1, else => 2 }; }
|
||
|
|
def runtime(x: u64): u8 { return match const x { 1 => 1, else => 2 }; }
|
||
|
|
def mixed<const M: bool>(): u8 { val v := M ? 1 as u8 : "a"; return v; }
|
||
|
|
def none<const N: u64>(): u8 { return match const N { 1 => 1, 2 => 2 }; }
|
||
|
|
def through<T>(ptr: T): castref<T> where T: pointer { return T::mutable ? &mut ptr[0] : &ptr[0]; }
|
||
|
|
|
||
|
|
def main(): void {
|
||
|
|
var x: i32 = 1;
|
||
|
|
through(&x as *i32) = 3;
|
||
|
|
val n := none::<3>();
|
||
|
|
}
|