when a function declares a type parameter:
fun <T> typedFunction(value: T, option: Option<T>) { ... }
how should I call the raw un-typed typedFunction in kotlin?
why?
In java I have:
// This is a method in an external library. I can not change it.
void <T> typedFunction(T t, Option<T> o) { ... }
// This is my code. optionsValues contains many types
// such as Option<Integer>, Option<String>, and ...
Map<Option<?>, ?> m = readAndParseFromConfigFile();
for (Map.Entry<Option<?>, ?> e : m.entrySet()) {
// The cast does the trick!
// I know my code is safe, I can tell the compiler to back off.
typedFunction((Option) e.getKey(), e.getValue());
}
Because typedFunction
declares a type named T
and binds both its parameters to this declared type, and on the call site I'm looping over multiple values whose exact type is unknown (but known to be safe, both parameters conform to same type) I can not call the typedFunction the way intended. I have to cast it into a raw type.
How can achieve the same in kotlin?
What IntelliJ does:
This is how IntelliJ converts my code:
val m: Map<Option<*>, *>? = ...
for ((key, value) in m!!) {
typedFunction<*>(key, value)
// ^^^ ERROR!!
}
But it gives an error afterwards: "Projections are not allowed on type arguments of functions and properties"
typedFunction(...)
Option<*>
andAny
which are incompatible