mirror of
https://github.com/Noratrieb/typed.git
synced 2026-01-14 08:35:05 +01:00
counting
This commit is contained in:
parent
b10ad7decc
commit
c2ce5dcae0
2 changed files with 45 additions and 1 deletions
42
src/count.rs
Normal file
42
src/count.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use std::marker::PhantomData;
|
||||
|
||||
trait Nat {
|
||||
fn int() -> i32;
|
||||
}
|
||||
|
||||
struct Z;
|
||||
|
||||
impl Nat for Z {
|
||||
fn int() -> i32 {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
struct S<N: Nat>(PhantomData<N>);
|
||||
|
||||
impl<N: Nat> Nat for S<N> {
|
||||
fn int() -> i32 {
|
||||
N::int() + 1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
trait Count: Nat {
|
||||
fn count() -> String;
|
||||
}
|
||||
|
||||
impl Count for Z {
|
||||
fn count() -> String {
|
||||
"0".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Count> Count for S<N> {
|
||||
fn count() -> String {
|
||||
format!("{} {}", N::count(), Self::int())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn count() {
|
||||
println!("{}", <S<S<S<Z>>> as Count>::count());
|
||||
}
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
mod count;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
count::count();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue