trait 的 + 是什麽意思?mbt 真的正確實現了嗎?

關於 trait,moonbit 的 + 和 Rust 的 + 是不是完全不同?如下述rs代碼:

trait A {}
trait B {}
struct S {}

// impl A for S {}
// impl B for S {}

trait C : A + B + Sized { fn bar(self) {} }
impl C for S {}


fn main() {
  let s = S {};
  s.bar();
}

會報錯:

the trait bound `S: A` is not satisfied
the trait bound `S: B` is not satisfied

但是 mbt 就不同了:

trait A { foo(Self) -> Unit }
trait B { foo(Self) -> Unit }
struct S {}
// impl A for S with foo(_) { println("A::foo") }  // 显式实现 A
// impl B for S with foo(_) { println("B::foo") }  // 显式实现 B
trait C : A + B { bar(Self) -> Unit }
impl C for S with bar(_) { println("C::bar") }  // 需要 A 和 B 已实现


fn main {
  let s = S::{}
  s.bar()
}

這段代碼居然編譯通過了!
所以 mbt 的 + 是什麽意思呢?這似乎和 rs 完全不一樣。

如果是把 S 传给一个需要 C bound 的多态函数,就会报错了。这里没报错是因为 .bar() 的解析跳过了 S : C 的完整解析直接去找了实现。这段代码理论上应该在 impl C for S 那里报错,这里没报是个 bug,近期会修复

1 个赞