2

私はこのscalaコードを見て、変数がステートメントpartitionedDataのどこから来ているのか混乱しています:case Some(partitionedData)

private def dispatchSerializedData(messages: Seq[KeyedMessage[K,Message]]): Seq[KeyedMessage[K, Message]] = {
    val partitionedDataOpt = partitionAndCollate(messages)
    partitionedDataOpt match {
      case Some(partitionedData) =>
        val failedProduceRequests = new ArrayBuffer[KeyedMessage[K,Message]]
        try {
          for ((brokerid, messagesPerBrokerMap) <- partitionedData) {
            if (logger.isTraceEnabled)
              messagesPerBrokerMap.foreach(partitionAndEvent =>
                trace("Handling event for Topic: %s, Broker: %d, Partitions: %s".format(partitionAndEvent._1, brokerid, partitionAndEvent._2)))
            val messageSetPerBroker = groupMessagesToSet(messagesPerBrokerMap)

            val failedTopicPartitions = send(brokerid, messageSetPerBroker)
            failedTopicPartitions.foreach(topicPartition => {
              messagesPerBrokerMap.get(topicPartition) match {
                case Some(data) => failedProduceRequests.appendAll(data)
                case None => // nothing
              }
            })
          }
        } catch {
          case t: Throwable => error("Failed to send messages", t)
        }
        failedProduceRequests
      case None => // all produce requests failed
        messages
    }
  }

試合中、その場で変数を作成しますか? partitionedDataOpt と同じですか?

4

1 に答える 1

2

partitionedDataOptは であり、または のOptionいずれかになります(どちらも のサブタイプです)Some(value)NoneOption

partitionedDataOptがオプションの場合Some、実際の値を内部でラップし、パターン マッチングpartitionedDataはオプションに含まれる値としてマークします。その場合、好きなように名前を付けることができ、一致したケースに対してローカルです。

試合中、その場で変数を作成しますか?

あなたはそう言うことができます、私は節partitionedDataでスコープ化されたローカル val として見ることができると思いますcase

partitionedDataOpt と同じですか?

partitionedDataOpt に値 (たとえば a )があるpartitionedDataOpt.get 場合と等しいと言えます。Some

OptionsちなみにSome、に固有のものではなく、None単に拡張するケースクラスですOptionsource

/** Class `Some[A]` represents existing values of type
 *  `A`.
 *
 *  @author  Martin Odersky
 *  @version 1.0, 16/07/2003
 */
final case class Some[+A](x: A) extends Option[A] {
  def isEmpty = false
  def get = x
}


/** This case object represents non-existent values.
 *
 *  @author  Martin Odersky
 *  @version 1.0, 16/07/2003
 */
case object None extends Option[Nothing] {
  def isEmpty = true
  def get = throw new NoSuchElementException("None.get")
}
于 2013-07-03T18:14:47.450 に答える