2016-04-27 117 views
-1

我有一個序列爲:斯卡拉序列[類型]錯誤時,模式匹配

def myMethod(mySeq: Seq[SomeType]) = mySeq match { 
    case Nil => // do someting 
    case _ => // do something else (error happens here) 
} 

當我運行這段代碼,我得到以下錯誤:

a type was inferred to be `Any`; this may indicate a programming error 

我從來沒有見過這個錯誤迄今。我在Scala 2.11上。我對這個錯誤是什麼感到無能爲力?任何線索?

編輯:這是我所受到質疑的實際方法:

def publishMessages(mySeq: Seq[MyData]): Future[Continue] = Future { 

    if (mySeq.nonEmpty) { 
     logger.info(s"sending a total of ${mySeq.length} for " + 
     s"metric ${mySeq.head.metric} messages to kafka topic ${producerConfig.topic}") 

     val jsonMessage = Json.stringify(Json.toJson(mySeq)) 
     val recordMetaDataF = Future { 
     scala.concurrent.blocking { 
      val recordMetaDataJavaFut = producer.send(
      new ProducerRecord[String, String](producerConfig.topic, jsonMessage) 
     ) 
      // if we don't make it to Kafka within 3 seconds, we timeout 
      recordMetaDataJavaFut.get(3, TimeUnit.SECONDS) 
     } 
     } 

     recordMetaDataF.recover { 
     case NonFatal(ex) => 
      logger.error("Exception while persisting data-points to kafka", ex) 
     } 
     Continue 
    } 
    else { 
     logger.debug(s"skip persisting to kafka topic ${producerConfig.topic} as no " + 
     " data-points were given to persist") 
     Continue 
    } 
    } 

這是我看到的時候我編譯警告:

[warn] Scala version was updated by one of library dependencies: 
[warn] * org.scala-lang:scala-library:(2.11.1, 2.11.7, 2.11.2, 2.11.6, 2.11.5, 2.11.0) -> 2.11.8 
[warn] To force scalaVersion, add the following: 
[warn] ivyScala := ivyScala.value map { _.copy(overrideScalaVersion = true) } 
[warn] Run 'evicted' to see detailed eviction warnings 

我仍然得到這個錯誤:

a type was inferred to be `Any`; this may indicate a programming error 
+3

你正在嘗試做什麼? –

+0

爲什麼這很重要?這兩個路徑都返回一個正確的和相同的類型。 IntelliJ不會抱怨這種方法! – sparkr

+1

因爲您發佈的代碼不需要用空白評論推斷任何內容,所以它不會產生任何錯誤。 –

回答

3

這與在您的應用中「做某事」意味着什麼:

scala> def myMethod(mySeq: Seq[String]) = mySeq match { 
|  case Nil => "" 
|  case _ => 12 
|  } 
myMethod: (mySeq: Seq[String])Any 

scala> def myMethod(mySeq: Seq[String]) = mySeq match { 
|  case Nil => "" 
|  case _ => "123" 
|  } 
myMethod: (mySeq: Seq[String])String 

正如你在第一種情況下看到的類型不對齊和編譯器推斷返回類型爲Any,第二他們都是字符串,返回類型爲String,你應該明確地註釋返回類型,並可能不會編譯(除非它是Any)。

+0

我編輯我的帖子!我仍然得到這個奇怪的錯誤! – sparkr

+0

你的編輯沒有任何錯誤,我懷疑你只是看着錯誤的行或者你發佈的代碼是不完整的,如果不是這樣,它不會編譯,因爲類型不匹配而不是給你一個警告(除非您以某種方式設置標誌,否則'Any'消息不是一個錯誤,而是一個警告。 –