scala.math.BigInt不能转换为java.lang.Long
调用此函数时出现上述错误scala.math.BigInt不能转换为java.lang.Long,scala,casting,sbt,long-integer,Scala,Casting,Sbt,Long Integer,调用此函数时出现上述错误 def filterOutNoInvPrices(storeNum: Long, inventoryMap: Map[Long, Long]) = { val inv = inventoryMap.get(storeNum) val minimumUnitsCheck:Boolean = inv.nonEmpty && inv.get.toInt > 3 println("what" + minimumUnitsCheck)
def filterOutNoInvPrices(storeNum: Long, inventoryMap: Map[Long, Long]) = {
val inv = inventoryMap.get(storeNum)
val minimumUnitsCheck:Boolean = inv.nonEmpty && inv.get.toInt > 3
println("what" + minimumUnitsCheck)
if (minimumUnitsCheck) {
Some(storeNum, inv.get)
}
else
None
}
更具体地说,它位于第3行inv.nonEmpty&&inv.get.toInt>3
。
我调用的函数是filterOutNoInvPrices
,它是getBasicStats
:
def getBasicStats(priceMap:Map[Long, String], invMap:Map[Long, Long])={
priceMap
.toSeq
.map(storeAndPrice => {
val price = getPriceAndPriceType(storeAndPrice._2)._1
(price, filterOutNoInvPrices(storeAndPrice._1, invMap))})
.filter(_._2.nonEmpty)
.map(record => (record._1, record._2.get))
}
在调用此函数之前,我显式地将inventoryMap设置为Map[Long,Long]
val invMap:Map[Long, Long] = inventoryMap
val pMap:Map[Long, String] = priceMap
val basicStats = getBasicStats(pMap, invMap)
我的代码编译成功。我在运行测试时遇到此错误。我确实通过了我自己的测试数据,但如果它是由数据引起的,那么数据映射是否应该更早失败?(例如,当我明确声明映射时发生的情况?)这是自下而上的
最近的堆栈跟踪是
scala.runtime.BoxesRunTime.untextolog(BoxesRunTime.java:105)
inventoryMap
有一个BigInt
而不是Long
。。。是的,如果使用正确,它应该会在某个地方失败,但是如果没有看到代码,很难从理论上解释问题所在。请发布错误堆栈跟踪,以便更容易看到发生了什么。根据您的定义,代码似乎没有失败HI@Dima,请查看我的更新。inventoryMap
来自哪里?您需要显示它是如何初始化的。FWIWval invMap:Map[Long,Long]=inventoryMap
对映射的内容没有任何影响,它只是创建了一个别名。非常感谢!!我认为它和其他语言一样是显式类型引用。是我的错。我以为它会失败。