Row Polymorphism(简称Row Poly)是一种多态的方式,举个例子。有如下Rust代码
trait MyEvent {
fn event();
}
struct EventStruct;
impl MyEvent for EventStruct {
fn event() {
//--snip--
}
}
struct OldEventStruct;
impl OldEventStruct {
fn event() {
//--snip--
}
}
fn dispatch_event(param : impl MyEvent) {
//--snip--
}
那么很显然,在Rust这种不支持Row Polymorphism的语言上,dispatch_event函数是无法使用OldEventStruct的。
让我们用支持Row Polymorphism的语言写一下这个示例
trait MyEvent {
def event()
}
class EventStruct extends MyEvent {
override def event() = {
}
}
class OldEvent {
def event() = {
}
}
class Dispatcher {
def dispatch_event((event : {def event()}) {
}
}
现在我们调用
dispatch_event(new EventStruct());
dispatch_event(new OldEvent());
编译通过。