如何在 Kotlin 中指示不同类型的数组?

How to indicate an array of different types in Kotlin?

我有这个函数,问题是所有方法(setString、setInt、...)的类型都是任何类型我得到一个错误:(

fun execStoredProcedure(storedProcedure: String, params: Array<Any>?) : ResultSet? {
     try {
        val procedure: CallableStatement = this.getConnection().prepareCall(storedProcedure)

         if ( params != null ) {
             for ( (index,param) in params.withIndex() ){
                 when( param::class.simpleName ){
                     "String" -> procedure.setString(index+1,param)
                     "Int"    -> procedure.setInt(index+1,param)
                     "Double" -> procedure.setDouble(index+1,param)
                     "Date"   -> procedure.setDate(index+1,param)
                 }
             }
         }

        return procedure.executeQuery()

    }catch (e:Error){
        println("Error in storedProcedure: ${e.message}")
        return null
    }
}
O si alguien sabe una funcion para poder ejecutar cualquier stored procedure en Kotlin, mi profesor no enseña nada :( 

如果您更改 when 子句以进行 class 比较而不是 String 名称比较,您可以利用 smart casting,这应该有效(未经测试):

when(param) {
    is String -> procedure.setString(index+1,param)
    is Int   -> procedure.setInt(index+1,param)
    is Double -> procedure.setDouble(index+1,param)
    is Date   -> procedure.setDate(index+1,param)
}