45 lines
1.4 KiB
Plaintext
45 lines
1.4 KiB
Plaintext
# Nothing implements a sealed trait, so no type or trait names it, it bounds
|
|||
|
|
# nothing, no type is one, and nothing refers to it. It holds only constants
|
||
|
|
# and types, each given here, and composes no trait. Its constants are read
|
||
|
|
# through it given its type arguments, and where one fails, what it tests
|
||
|
|
# fails. A trait constant's default is taken only by a type naming the trait.
|
||
|
|
@sealed pub trait closes<T> { const value := T is closeable; }
|
||
|
|
@sealed pub trait owns<T> { const value := T is owning; }
|
||
|
|
trait shown { def show(&this): void; }
|
||
|
|
@sealed trait wrong : shown {
|
||
|
|
def f(): u8;
|
||
|
|
const bare: u64;
|
||
|
|
type open;
|
||
|
|
type bounded: copyable = u8;
|
||
|
|
}
|
||
|
|
trait sized { const n: u64 = 4; }
|
||
|
|
|
||
|
|
struct implements : closes<u8> { var x: u8; }
|
||
|
|
trait composes : closes<u8> {}
|
||
|
|
def bounded<T>(x: T): void where T: closes<u8> {}
|
||
|
|
def tests<T>(x: T): u8 {
|
||
|
|
if const T is closes<u8> { return 1; }
|
||
|
|
match const T {
|
||
|
|
closes<u8> => return 2;
|
||
|
|
else => {}
|
||
|
|
}
|
||
|
|
return 3;
|
||
|
|
}
|
||
|
|
def refers(r: &closes<u8>): void {}
|
||
|
|
def unnamed(): bool { return closes::value; }
|
||
|
|
def counted(): bool { return closes::<u8, u8>::value; }
|
||
|
|
type pick<T> = T is closes<u8> ? u8 : u16;
|
||
|
|
def value_test(x: u8): bool { return x is closes<u8>; }
|
||
|
|
|
||
|
|
def shut<T>(x: &mut T): void {
|
||
|
|
if const !owns::<T>::value {
|
||
|
|
x.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
struct shaped { var a: u8; }
|
||
|
|
def total<T>(): u64 where T: sized { return T::n; }
|
||
|
|
|
||
|
|
def main(): void {
|
||
|
|
val n := total::<shaped>();
|
||
|
|
}
|