【发布时间】:2025-12-27 23:10:15
【问题描述】:
我想在表的一行中找到所有值的最小值(可能有空值,列可能是不同的类型——Int、Long、Double、Boolean)。
函数的返回类型取决于输入列,如果都是 Int 则返回类型应该是 Int。如果混合不同类型,则加倍,等等。
我知道 returnType 是什么。
方法如下:
def findMin(args: List[Any]): Any =
一种可能的解决方案是:
returnType match {
case IntType =>
minValInt = Int.Min
for(args){
check for not null
for(arg <- args) {
temp: Int = arg match {
case x: Int => x
case x: Boolean => convert to Int
}
minValInt = min(minValInt, temp)
return minValInt
case LongType =>
minValLong = Long.Min
for(args){
check for not null -> minValLong = min(minValLong, arg) (some restrictions while using the arg value directly)
}
return minValLong
case DoubleType =>
minValDouble = Double.Min
for(arg <- args){
check for not null
temp: Double = arg match {
case x: Int => convert to double
case x: Long => convert to double (some restrictions while converting)
case x: Double => x
case x: Boolean => convert to double
}
minValDouble = min(minValDouble, temp)
}
return minValDouble
}
这可以在单个匹配情况下完成还是更好的“更整洁”?
【问题讨论】:
-
你不能让一个函数根据它的参数返回不同的类型,所以它会返回 Any 所以你会在某个地方进行强制转换。都有点乱。用例是什么?您将返回的最小值用于什么用途?
-
@ArchetypalPaul 当然可以。它被称为泛型 ;) (或任何东西在 scala 中的花哨名称)。
-
@Dima - 当然你不能。泛型是关于在编译时基于参数 TYPES 的类型计算。这个问题是关于在运行时根据动态识别的值类型计算返回类型。
-
@Det,我只是说上一条评论中的“您不能让单个函数根据其参数返回不同类型的函数”的说法是错误的。我们同意吗?您的陈述也好不到哪里去 :) 首先,您误读了这个问题:OP 说,他 知道 返回类型应该是什么。其次,看你怎么错了,简单地考虑这个函数:
def identity[T](x: T) = x它的返回类型是不同的,取决于参数。这只是一个最简单的例子,当然,您可以使用 manifests 和implicits 做更多更奇特的事情。 -
因为“Any”是 Scala 中所有类型的超集。因此,根据参数返回不同的类型是可行的。
标签: scala