我想在Rust中实现Y combinator:
trait Loop<T>: Fn(&Self) -> T {}
impl<T> Loop<T> for Fn(&Loop<T>) -> T {}
fn main() {
let y = |f| { |w| { w(w) }(|w| f(w(w))) };
}
我的第一个目标是让代码编译,所以我忽略了这里的call-by-name问题。编译Rust Playground的输出:
rustc 1.19.0-nightly (e2eaef849 2017-06-11)
error[E0038]: the trait `Loop` cannot be made into an object
--> <anon>:4:25
|
4 | impl<T> Loop<T> for Fn(&Loop<T>) -> T {
| ^^^^^^^ the trait `Loop` cannot be made into an object
|
= note: the trait cannot use `Self` as a type parameter in the supertraits or where-clauses
我明白为什么遇到麻烦,但我该如何修复并使其编译?
更新
这个问题不是链接问题的重复,因为这个问题是关于编写Y组合子,而不仅仅是一个简单的修复点函数。实际上,那里发布的答案都没有关于编写修正点功能,而不是组合器。如果没有使用任何直接递归,组合器必须完全在闭包中实现。