我有以下函数(它在protobuf对象MyRequest上工作
def createRequestFromJson(requestJson: String): MyRequest = {
val protoJson = getResource(requestJson)
JsonFormat.fromJsonString[MyRequest](protoJson)
}我想用一个不同的对象重用这个函数,所以我添加了一个类型
def createRequestFromJson[A](requestJson: String): A = {
val protoJson = getResource(requestJson)
JsonFormat.fromJsonString[A](protoJson)
}但是我得到了一个错误
Error:(68, 30) type arguments [A] do not conform to method fromJsonString's type parameter bounds [A <: scalapb.GeneratedMessage with scalapb.Message[A]]
JsonFormat.fromJsonString[A](protoJson)我试着把定义改成
def createResponseFromJson[A <: scalapb.GeneratedMessage with scalapb.Message[A]](protoJsonFile: String): A = {但仍然会给出更多的错误
我做错了什么?
发布于 2018-06-04 08:05:47
JsonFormat.fromJsonString需要隐式GeneratedMessageCompanion。如果您将签名更改为:
def createResponseFromJson[A <: scalapb.GeneratedMessage with scalapb.Message[A]
: GeneratedMessageCompanion](protoJsonFile: String): Ahttps://stackoverflow.com/questions/50664598
复制相似问题