【发布时间】:2023-03-14 11:24:01
【问题描述】:
在 Java 中给出以下内容:
public interface Reply<T> {
T data();
}
public class StatusReply implements Reply<String> {
private final String status;
public StatusReply(String status) {
this.status = status;
}
@Override
public String data() {
return status;
}
}
我希望能够在 Scala 中做到这一点:
class PassthroughReply[R <: Reply[_]](val reply: R)
extends Reply[T] { // won't compile, `T` type not found
override
def data[T : Reply]: T = reply.data
}
val statusReply = new StatusReply("OK")
val passthroughReply = new PassthroughReply[SatusReply](statusReply)
passthroughReply.data // Should return "OK"
我想要的是PassthroughReply 实例中的data 应该与Reply 的包装子类型的data 具有相同的类型。
【问题讨论】:
标签: scala generics scala-2.10 generic-programming