Kotlin
Type parameter can have 'in' or 'out' variance
Info
New
Last modified: 03 December 2024 Reports type parameters that can have in
or out
variance.
Using in
and out
variance provides more precise type inference in Kotlin and clearer code semantics.
Example:
class Box<T>(val obj: T)
fun consumeString(box: Box<String>) {}
fun consumeCharSequence(box: Box<CharSequence>) {}
fun usage(box: Box<String>) {
consumeString(box)
consumeCharSequence(box) // Compilation error
}
The quick-fix adds the matching variance modifier:
class Box<out T>(val obj: T)
fun consumeString(box: Box<String>) {}
fun consumeCharSequence(box: Box<CharSequence>) {}
fun usage(box: Box<String>) ++{
consumeString(box)
consumeCharSequence(box) // OK
}
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
AddVarianceModifier
Inspection Details | |
---|---|
By default bundled with: | |
Can be installed with plugin: | Kotlin, 243.23126-IJ |
Thanks for your feedback!
Was this page helpful?