0

I have following code snippet:

  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

What is the type of the value from Map(String, ????)? The type value of the case is very mixed:

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  

Some are Strings and another are Integers.

softshipper
  • 32,463
  • 51
  • 192
  • 400

2 Answers2

1

I think it is AnyRef because that's what java class types usually roll up to. Also, in java they meet at the object level.

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

Why cannot cast Integer to String in java?

uh_big_mike_boi
  • 3,350
  • 4
  • 33
  • 64
1

An easy way to check for yourself is to give a clearly incorrect type:

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

You'll see an error like the following:

Error:(*line*, *column*) type mismatch;
 found   : *the type you are interested in*
 required: Int
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487