I am new to Kotlin. I am facing the following problem. I have a function like this,
fun updateAfterFind(updateColumn:Map<Column<*>, Any?>): Boolean{
ExposedUser.update( {ExposedUser.id eq 123} ){
for (column in updateColumn){
it[column.key] = column.value
}
}
return true;
}
The column.value
is of type Any?. Still I am getting the following error,
I have also tried casting, column.name as Any?
. But it didn't helped.
After doing gradle build
I got the following error,
Type inference failed: Cannot infer type parameter S in fun <S> set(column: Column<S>, value: S?): Unit
None of the following substitutions
(Column<CapturedTypeConstructor(*)>,CapturedTypeConstructor(*)?)
(Column<Any>,Any?)
can be applied to
(Column<*>,Any)
The signature for <S> UpdateStatement.set(Column<S>,S?)
says that the type parameter for the column has to match the value type.
You have a Column<*>
and a value Any
. That doesn't match, because Column<*>
is not the same as Column<Any>
. The *
could be anything, so it could be a Column<Integer>
, for example, and you can't set an Any
as the value.
If you know that the types really are compatible, then you can override the check by saying it[column.key as Column<Any>] = column.value
The cast is gross, but using that Map
to store these updates is not type-safe, and this is where you have to take responsibility for that.