地图的价值类型是什么?

时间:2017-11-15 08:52:11

标签: scala

我有以下代码段:

  private def buildProps(host: => String)(config: => List[KafkaConfig]) =
    (config.map {
      case ClientId(value: String) =>
        (ProducerConfig.CLIENT_ID_CONFIG, value)
      case Acks(value: String) =>
        (ProducerConfig.ACKS_CONFIG, value)
      case Retries(value) =>
        (ProducerConfig.RETRIES_CONFIG, value)
      case BatchSize(value) =>
        (ProducerConfig.BATCH_SIZE_CONFIG, value)
      case LingerMs(value) =>
        (ProducerConfig.LINGER_MS_CONFIG, value)
      case BufferMemory(value) =>
        (ProducerConfig.BUFFER_MEMORY_CONFIG, value)
    } ++ List(
      (ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, host),
      (ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.ByteArraySerializer"),
      (ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.ByteArraySerializer"))
      ).toMap

来自Map(String, ????)的值的类型是什么? 案件的类型value非常复杂:

final case class ClientId(value: String) extends KafkaConfig

final case class Acks(value: String) extends KafkaConfig

final case class Retries(value: java.lang.Integer) extends KafkaConfig

final case class BatchSize(value: java.lang.Integer) extends KafkaConfig

final case class LingerMs(value: java.lang.Integer) extends KafkaConfig

final case class BufferMemory(value: java.lang.Integer) extends KafkaConfig  

有些是Strings,有些是Integers

2 个答案:

答案 0 :(得分:1)

我认为它是AnyRef,因为那是java类类型通常所涉及的内容。 此外,在java中,它们在对象级别相遇。

https://www.scala-lang.org/old/node/71%3Fsize=_original.html#

Why cannot cast Integer to String in java?

答案 1 :(得分:1)

检查自己的简单方法是给出一个明显不正确的类型:

private def buildProps(host: => String)(config: => List[KafkaConfig]): Int = ...

您会看到如下错误:

Error:(*line*, *column*) type mismatch;
 found   : *the type you are interested in*
 required: Int