模式匹配时Scala Seq[Type]错误
我有一个序列号:模式匹配时Scala Seq[Type]错误,scala,Scala,我有一个序列号: 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 到目前为止,我从未
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
这与你的应用程序中“做点什么”的含义有关:
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
).你还想做什么?为什么这很重要?这两个路径都返回一个正确且相同的类型。IntelliJ不会抱怨这种方法!因为你发布的代码不需要推断任何带有空白注释的内容,所以不会产生错误。请看我的帖子!你会感到困惑的!哪一行给出了错误?你的第一个抽象代码使用match,你的“真实”代码不使用match,因此很难猜测到底发生了什么你的编辑没有错,我怀疑你只是看错了行,或者你发布的代码不完整,如果不是这种情况,它将根本不会编译,因为类型不匹配,而不会向您发出警告(除非您以某种方式设置标志,Any
消息是警告而不是错误)。