You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/md/kotlin.core/declarations.md
+11-11
Original file line number
Diff line number
Diff line change
@@ -1786,27 +1786,27 @@ See [the type system section][Mixed-site variance] for details.
1786
1786
1787
1787
We would consider a type expression `E` to be **covariant**in type parameter `X` iffor every concrete type expressions `T1` and `T2` such that `T1<:T2` it holds that `E[T1/X] <:E[T2/X]`. One could also say, that the *position* `X` takes in `E` is**covariant**.
1788
1788
1789
-
Not nested covariant cases are the following:
1789
+
Base covariant cases are the following:
1790
1790
1791
1791
-A type expression is covariant in type parameter `X`, ifthis parameter is explicitly declared as `out` either [in the expression itself][Use-site variance] or [in the type declaration][Declaration-site variance];
1792
1792
-Types of functions are covariant in their return type parameter, since functions have [types of kind][Function types] `FN<inA1, ..., inAN, outR>`);
1793
1793
-Types of read-only properties are effectively in covariant position;
1794
1794
1795
1795
In the same way, we would consider a type expression `E` to be **contravariant**in type parameter `X` iffor every concrete type expressions `T1` and `T2` such that `T1<:T2` it holds that `E[T2/X] <:E[T1/X]`.
1796
1796
1797
-
Not nested contravariant cases are the following:
1797
+
Base contravariant cases are the following:
1798
1798
1799
1799
-A type expression is contravariant in type parameter `X`, ifthis parameter is explicitly declared as `in` either [in the expression itself][Use-site variance] or [in the type declaration][Declaration-site variance];
1800
1800
-Types of functions are contravariant in their argument types, since functions have [types of kind][Function types] `FN<inA1, ..., inAN, outR>`;
1801
1801
1802
1802
Lastly, we would consider a type expression `E` to be **invariant**in type parameter `X` iffor every concrete type expressions `T1` and `T2`, `E[T1/X] <:E[T2/X]` holds ifand only if `T1=T2` (`T1<:T2` and `T2<:T1`).
1803
1803
1804
-
Not nested invariant cases are the following:
1804
+
Base invariant cases are the following:
1805
1805
1806
1806
-A type expression is invariant in type parameter `X`, ifthis parameter isnot declared as either `in` or `out` [in the expression itself][Use-site variance] or [in the type declaration][Declaration-site variance];
1807
1807
-Types of mutable properties are effectively in invariant position;
1808
1808
1809
-
We will use `+` as a symbol denoting contravariance, `-` as a symbol denoting covarianceand `o` as a symbol denoting invariance.
1809
+
We will use `+` as a symbol denoting covariance, `-` as a symbol denoting contravarianceand `o` as a symbol denoting invariance.
1810
1810
1811
1811
To infer variance of a complex type expression in a type parameter, variance composition is used, which is defined as follows:
1812
1812
@@ -1815,7 +1815,7 @@ To infer variance of a complex type expression in a type parameter, variance com
1815
1815
\begin{align*}
1816
1816
\texttt{+} \otimes x &= x \text{, where $x$ is any variance} \\
1817
1817
\texttt{-} \otimes \texttt{+} &= \texttt{-} \\
1818
-
\texttt{-} \otimes \texttt{-} &= \texttt{-} \\
1818
+
\texttt{-} \otimes \texttt{-} &= \texttt{+} \\
1819
1819
\texttt{o} \otimes x &= \texttt{o} \text{, where $x$ is any variance} \\
1820
1820
x \otimes \texttt{o} &= \texttt{o} \text{, where $x$ is any variance}
1821
1821
\end{align*}
@@ -1834,14 +1834,14 @@ When a type parameter has declared variance, and is used in a position of a diff
1834
1834
-A covariant type parameter is used as an argument to a contravariant position;
1835
1835
-A contravariant type parameter is used as an argument to a covariant position;
1836
1836
1837
-
Important remark: variance conflict does not occur if the containing declaration isprivate to the type parameter owner (in which case its visibility is restricted, see the [visibility][Declaration visibility] section for details).
1837
+
>Important remark: variance conflict does not occur if the containing declaration isprivate to the type parameter owner (in which case its visibility is restricted, see the [visibility][Declaration visibility] section for details).
1838
1838
1839
1839
This applies only to member declarations of the corresponding class, extensions are not subject to this limitation.
1840
1840
1841
-
This restrictions may be lifted in particular cases by [annotating][Annotations] the corresponding type parameter usage with a special built-inannotation `kotlin.UnsafeVariance`.
1841
+
These restrictions may be lifted in particular cases by [annotating][Annotations] the corresponding type parameter usage with a special built-inannotation `kotlin.UnsafeVariance`.
1842
1842
By supplying thisannotation the author of the code explicitly declares that safety features that variance checks provide are not needed inthis particular declarations.
1843
1843
1844
-
>BasicExamples:
1844
+
>Basicexamples:
1845
1845
>
1846
1846
> ```kotlin
1847
1847
>interfaceHolder<T>
@@ -1888,13 +1888,13 @@ By supplying this annotation the author of the code explicitly declares that saf
1888
1888
> ```kotlin
1889
1889
>classFoo<inT>() // T is contravariant
1890
1890
>
1891
-
>classBar<inS>() { //T is contravariant
1891
+
>classBar<inS>() { //S is contravariant
1892
1892
>funbar(x:Foo<S>) {} // ERROR, contravariant parameter in covariant usage
1893
1893
>// bar has type F1<in Foo<S>, out Unit>
1894
1894
>// Need to find variance of F1<in Foo<S>, out Unit> in S
>// Variance of F1<in Foo<S>, out Unit> in S -- covariance
1899
1899
> }
1900
1900
>
@@ -1911,7 +1911,7 @@ By supplying this annotation the author of the code explicitly declares that saf
1911
1911
> }
1912
1912
> ```
1913
1913
>
1914
-
>Remark: cases `e`, `f`, `f2`, `g`, `g2` inBasicExamples are also examples of variance composition, since they are read-only fields. However, since read-only fields are of covariance (`+`) andfor any variance $x$, $\texttt{+} \otimes x = x$, it was ommited at that point.
1914
+
>Remark: cases `e`, `f`, `f2`, `g`, `g2` in"Basic examples" are also examples of variance composition, since they are read-only fields. However, since read-only fields are of covariance (`+`) andfor any variance $x$, $\texttt{+} \otimes x = x$, it was omitted at that point.
1915
1915
>
1916
1916
>Any of these restrictions may be lifted using `@UnsafeVariance` annotation on the type argument:
0 commit comments