Suppose that type A inherits type B or implements interface B.
Now suppose that I define a function
fun f(myArgument: Array<B>)
somewhere else I declare some variable myArray
of type Array<A>
.
Now a direct call
f(myArray)
causes a compilation error.
- What is the reason for this? I think it is acceptable Java code.
- What is the workaround if I want to translate the equivalent Java code into Kotlin code? (Auto-translate results in code that can not compile). Using
f(myArray as Array<B>)
solved the problem in the few cases I’ve tried, but is it always guaranteed to work?
Thanks.
PS: Sorry if I am missing something trivial - Java/Kotlin noob here.