尝试编写类似于 Haskell 的 HList 的东西,具有按类型搜索的能力。使用以下代码,在 play.rust-lang.org 版本中rustc 0.13.0-dev (567b90ff0 2014-12-13 20:02:15 +0000)
出现错误:
<anon>:35:26: 35:31 error: unable to infer enough type information to locate the impl of the trait `ContainsRel<int, _>` for the type `HCons<int, HNil>`; type annotations required
<anon>:35 let foo: &int = list.get();
^~~~~
我不确定为什么它不能推断出正确的类型,HNil 没有 ContainsRel 实现。
trait HList {}
struct HNil;
struct HCons<H, T: HList>(H, T);
impl HList for HNil {}
impl<H, T: HList> HList for HCons<H, T> {}
trait Peano {}
struct Zero;
struct Succ<N: Peano>(N);
impl Peano for Zero {}
impl<N: Peano> Peano for Succ<N> {}
trait ContainsRel<E, P: Peano> {
fn get(&self) -> &E;
}
impl<E, L: HList> ContainsRel<E, Zero> for HCons<E, L> {
fn get(&self) -> &E {
&self.0
}
}
impl<E, X, L: ContainsRel<E, P>+HList, P: Peano> ContainsRel<E, Succ<P>> for HCons<X, L> {
fn get(&self) -> &E {
self.1.get()
}
}
fn main() {
let list: HCons<uint, HCons<int, HNil>> = HCons(5u, HCons(6i, HNil));
let foo: &int = list.get();
}