我有以下代码段:
def determineProducerType(keySerializer: KkSerializer)(valueSerializer: KkSerializer)(props: Properties)
: Eval[KafkaProducer[java.lang.Object, java.lang.Object]] = (keySerializer, valueSerializer) match {
case (KkStringSeDe, KkStringSeDe) => Later(new KafkaProducer[String, String](props))
case (KkStringSeDe, KkByteArraySeDe) => Later(new KafkaProducer[String, Byte](props))
case (KkStringSeDe, KkIntegerSeDe) => Later(new KafkaProducer[String, Integer](props))
case (KkStringSeDe, KkLongSeDe) => Later(new KafkaProducer[String, Long](props))
}
编译器抱怨:
[info] Compiling 2 Scala sources to /home/developer/Desktop/scala/PureProducer/target/scala-2.12/classes ...
[error] /home/developer/Desktop/scala/PureProducer/src/main/scala/TheProducer.scala:113:48: type mismatch;
[error] found : org.apache.kafka.clients.producer.KafkaProducer[String,String]
[error] required: org.apache.kafka.clients.producer.KafkaProducer[Object,Object]
[error] Note: String <: Object, but Java-defined class KafkaProducer is invariant in type K.
[error] You may wish to investigate a wildcard type such as `_ <: Object`. (SLS 3.2.10)
[error] Note: String <: Object, but Java-defined class KafkaProducer is invariant in type V.
[error] You may wish to investigate a wildcard type such as `_ <: Object`. (SLS 3.2.10)
[error] case (KkStringSeDe, KkStringSeDe) => Later(new KafkaProducer[String, String](props))
[error] ^
[error] /home/developer/Desktop/scala/PureProducer/src/main/scala/TheProducer.scala:114:51: type mismatch;
[error] found : org.apache.kafka.clients.producer.KafkaProducer[String,Byte]
[error] required: org.apache.kafka.clients.producer.KafkaProducer[Object,Object]
[error] case (KkStringSeDe, KkByteArraySeDe) => Later(new KafkaProducer[String, Byte](props))
[error] ^
[error] /home/developer/Desktop/scala/PureProducer/src/main/scala/TheProducer.scala:115:49: type mismatch;
[error] found : org.apache.kafka.clients.producer.KafkaProducer[String,Integer]
[error] required: org.apache.kafka.clients.producer.KafkaProducer[Object,Object]
[error] Note: String <: Object, but Java-defined class KafkaProducer is invariant in type K.
[error] You may wish to investigate a wildcard type such as `_ <: Object`. (SLS 3.2.10)
[error] Note: Integer <: Object, but Java-defined class KafkaProducer is invariant in type V.
[error] You may wish to investigate a wildcard type such as `_ <: Object`. (SLS 3.2.10)
[error] case (KkStringSeDe, KkIntegerSeDe) => Later(new KafkaProducer[String, Integer](props))
[error] ^
[error] /home/developer/Desktop/scala/PureProducer/src/main/scala/TheProducer.scala:116:46: type mismatch;
[error] found : org.apache.kafka.clients.producer.KafkaProducer[String,Long]
[error] required: org.apache.kafka.clients.producer.KafkaProducer[Object,Object]
[error] case (KkStringSeDe, KkLongSeDe) => Later(new KafkaProducer[String, Long](props))
[error] ^
[error] four errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 2 s, completed Nov 12, 2017 10:39:14 AM
我要做的是,我定义了一个总和类型:
sealed trait KkSerializer
case object KkStringSeDe extends KkSerializer
case object KkByteArraySeDe extends KkSerializer
case object KkIntegerSeDe extends KkSerializer
case object KkLongSeDe extends KkSerializer
当匹配适当的和类型时,它应该返回相应的类型。
创建KafkaProducer
的实例是:
val producer = new KafkaProducer[String, String](props)
如何解决?
答案 0 :(得分:1)
我认为在这种情况下,你可以使用路径依赖类型来获得你想要的东西:
sealed trait KkSerializer { type Out }
case object KkStringSeDe extends KkSerializer {
type Out = String
}
case object KkByteArraySeDe extends KkSerializer {
type Out = Byte
}
def determineProducerType(k: KkSerializer)(v: KkSerializer)(props: Properties): Eval[KafkaProducer[k.Out, v.Out]] =
Later(new KafkaProducer[k.Out, v.Out](props))