Skip to content

Commit 943a891

Browse files
committed
PEP 695: Fix Kotlin/Java details and add Dart
1 parent d927bbd commit 943a891

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed

pep-0695.rst

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,9 @@ Java
896896
----
897897

898898
Java uses angle brackets to declare type parameters and for specialization.
899-
The "extends" keyword is used to specify an upper bound.
899+
By default, type parameters are invariant.
900+
The "extends" keyword is used to specify an upper bound. The "super" keyword
901+
is used to specify a contravariant bound.
900902

901903
Java uses use-site variance. The compiler places limits on which methods and
902904
members can be accessed based on the use of a generic type. Variance is
@@ -912,6 +914,8 @@ Java provides no way to specify a default type argument.
912914

913915
// Generic method
914916
public <S extends Number> void method1(S value) { }
917+
// Use site variance
918+
public void method1(ClassA<? super Integer> value) { }
915919
}
916920
917921

@@ -955,7 +959,7 @@ specialization. The "extends" keyword is used to specify a bound. It can be
955959
combined with other type operators such as "keyof".
956960

957961
TypeScript uses declaration-site variance. Variance is inferred from
958-
usage, not specified explicitly. TypeScript 4.7 will introduce the ability
962+
usage, not specified explicitly. TypeScript 4.7 introduced the ability
959963
to specify variance using "in" and "out" keywords. This was added to handle
960964
extremely complex types where inference of variance was expensive.
961965

@@ -1036,8 +1040,7 @@ Swift
10361040
Swift uses angle brackets to declare type parameters and for specialization.
10371041
The upper bound of a type parameter is specified using a colon.
10381042

1039-
Swift uses declaration-site variance, and variance of type parameters is
1040-
inferred from their usage.
1043+
Swift doesn't support generic variance, all type parameters are invariant.
10411044

10421045
Swift provides no way to specify a default type argument.
10431046

@@ -1105,7 +1108,9 @@ Kotlin
11051108
------
11061109

11071110
Kotlin uses angle brackets to declare type parameters and for specialization.
1108-
The upper bound of a type is specified using a colon.
1111+
By default, type parameters are invariant. The upper bound of a type is
1112+
specified using a colon. Alternatively a "where" clause can specify various
1113+
constraints.
11091114

11101115
Kotlin supports declaration-site variance where variance of type parameters is
11111116
explicitly declared using "in" and "out" keywords. It also supports use-site
@@ -1116,19 +1121,19 @@ Kotlin provides no way to specify a default type argument.
11161121
::
11171122

11181123
// Generic class
1119-
class ClassA<T> { }
1124+
class ClassA<T>
11201125

11211126
// Type parameter with upper bound
1122-
class ClassB<T: SomeClass1> { }
1127+
class ClassB<T : SomeClass1>
11231128

11241129
// Contravariant and covariant type parameters
1125-
class ClassC<in S, out T> { }
1130+
class ClassC<in S, out T>
11261131

11271132
// Generic function
1128-
fun func1<T>() -> T {}
1133+
fun <T> func1(): T { }
11291134

11301135
// Generic type alias
1131-
typealias<T> = ClassA<T>
1136+
typealias TypeAliasFoo<T> = ClassA<T>
11321137

11331138

11341139
Julia
@@ -1151,6 +1156,37 @@ upper and lower bounds on a type.
11511156
// Alternate form of generic function
11521157
function func2(v::Container{T} where T <: Real)
11531158

1159+
Dart
1160+
-----
1161+
1162+
Dart uses angle brackets to declare type parameters and for specialization.
1163+
The upper bound of a type is specified using the "extends" keyword. By default,
1164+
type parameters are covariant.
1165+
1166+
Dart supports declaration-site variance where variance of type parameters is
1167+
explicitly declared using "in", "out" and "inout" keywords. It does not support
1168+
use-site variance.
1169+
1170+
Dart provides no way to specify a default type argument.
1171+
1172+
::
1173+
1174+
// Generic class
1175+
class ClassA<T> { }
1176+
1177+
// Type parameter with upper bound
1178+
class ClassB<T extends SomeClass1> { }
1179+
1180+
// Contravariant and covariant type parameters
1181+
class ClassC<in S, out T> { }
1182+
1183+
// Generic function
1184+
T func1<T>() { }
1185+
1186+
// Generic type alias
1187+
typedef TypeDefFoo<T> = ClassA<T>;
1188+
1189+
11541190

11551191
Summary
11561192
-------
@@ -1162,7 +1198,8 @@ Summary
11621198
| C++ | template | n/a | n/a | = | n/a | n/a |
11631199
| | <> | | | | | |
11641200
+------------+----------+---------+--------+----------+-----------+-----------+
1165-
| Java | <> | extends | | | use | inferred |
1201+
| Java | <> | extends | | | use | super, |
1202+
| | | | | | | extends |
11661203
+------------+----------+---------+--------+----------+-----------+-----------+
11671204
| C# | <> | where | | | decl | in, out |
11681205
+------------+----------+---------+--------+----------+-----------+-----------+
@@ -1171,15 +1208,19 @@ Summary
11711208
+------------+----------+---------+--------+----------+-----------+-----------+
11721209
| Scala | [] | T <: X | T >: X | | use, decl | +, - |
11731210
+------------+----------+---------+--------+----------+-----------+-----------+
1174-
| Swift | <> | T: X | | | decl | inferred |
1211+
| Swift | <> | T: X | | | n/a | n/a |
11751212
+------------+----------+---------+--------+----------+-----------+-----------+
11761213
| Rust | <> | T: X, | | = | decl | inferred, |
11771214
| | | where | | | | explicit |
11781215
+------------+----------+---------+--------+----------+-----------+-----------+
1179-
| Kotlin | <> | T: X | | | use, decl | inferred |
1216+
| Kotlin | <> | T: X, | | | use, decl | in, out |
1217+
| | | where | | | | |
11801218
+------------+----------+---------+--------+----------+-----------+-----------+
11811219
| Julia | {} | T <: X | X <: T | | n/a | n/a |
11821220
+------------+----------+---------+--------+----------+-----------+-----------+
1221+
| Dart | <> | extends | | | decl | in, out, |
1222+
| | | | | | | inout |
1223+
+------------+----------+---------+--------+----------+-----------+-----------+
11831224
| Python | [] | T: X | | | decl | inferred |
11841225
| (proposed) | | | | | | |
11851226
+------------+----------+---------+--------+----------+-----------+-----------+

0 commit comments

Comments
 (0)