The trade-off of “declaration-site variance” (compared to what Java does) is interesting (by marking generic parameters in or out). However, there is no way to mark function parameters as in or out, so it assumes that function parameters are necessarily input parameters (for simplicity I guess).
As a consequence, this is not possible:
interface Source<out T> {
fun copyTo(to: Collection<T>)
}
But worse, if we don’t constraint T to be an output parameter so that it compiles:
interface Source<T> {
fun copyTo(to: Collection<T>)
}
then the compiled code is incorrect (kotlinc a.kt && procyon Source):
public interface Source<T>
{
void copyTo(@NotNull final Collection<? extends T> p0);
}
The expected parameter type is Collection<? super T>, not Collection<? extends T> (semantically, it’s an output parameter).
Is there any way to mark a parameter as “output” (to override the default behavior which always considers parameters as “input”) regarding the variance rules?