896
896
----
897
897
898
898
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.
900
902
901
903
Java uses use-site variance. The compiler places limits on which methods and
902
904
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.
912
914
913
915
// Generic method
914
916
public <S extends Number> void method1(S value) { }
917
+ // Use site variance
918
+ public void method1(ClassA<? super Integer> value) { }
915
919
}
916
920
917
921
@@ -955,7 +959,7 @@ specialization. The "extends" keyword is used to specify a bound. It can be
955
959
combined with other type operators such as "keyof".
956
960
957
961
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
959
963
to specify variance using "in" and "out" keywords. This was added to handle
960
964
extremely complex types where inference of variance was expensive.
961
965
@@ -1036,8 +1040,7 @@ Swift
1036
1040
Swift uses angle brackets to declare type parameters and for specialization.
1037
1041
The upper bound of a type parameter is specified using a colon.
1038
1042
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.
1041
1044
1042
1045
Swift provides no way to specify a default type argument.
1043
1046
@@ -1105,7 +1108,9 @@ Kotlin
1105
1108
------
1106
1109
1107
1110
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.
1109
1114
1110
1115
Kotlin supports declaration-site variance where variance of type parameters is
1111
1116
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.
1116
1121
::
1117
1122
1118
1123
// Generic class
1119
- class ClassA<T> { }
1124
+ class ClassA<T>
1120
1125
1121
1126
// Type parameter with upper bound
1122
- class ClassB<T: SomeClass1> { }
1127
+ class ClassB<T : SomeClass1>
1123
1128
1124
1129
// Contravariant and covariant type parameters
1125
- class ClassC<in S, out T> { }
1130
+ class ClassC<in S, out T>
1126
1131
1127
1132
// Generic function
1128
- fun func1 <T>() -> T {}
1133
+ fun <T> func1(): T { }
1129
1134
1130
1135
// Generic type alias
1131
- typealias<T> = ClassA<T>
1136
+ typealias TypeAliasFoo <T> = ClassA<T>
1132
1137
1133
1138
1134
1139
Julia
@@ -1151,6 +1156,37 @@ upper and lower bounds on a type.
1151
1156
// Alternate form of generic function
1152
1157
function func2(v::Container{T} where T <: Real)
1153
1158
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
+
1154
1190
1155
1191
Summary
1156
1192
-------
@@ -1162,7 +1198,8 @@ Summary
1162
1198
| C++ | template | n/a | n/a | = | n/a | n/a |
1163
1199
| | <> | | | | | |
1164
1200
+------------+----------+---------+--------+----------+-----------+-----------+
1165
- | Java | <> | extends | | | use | inferred |
1201
+ | Java | <> | extends | | | use | super, |
1202
+ | | | | | | | extends |
1166
1203
+------------+----------+---------+--------+----------+-----------+-----------+
1167
1204
| C# | <> | where | | | decl | in, out |
1168
1205
+------------+----------+---------+--------+----------+-----------+-----------+
@@ -1171,15 +1208,19 @@ Summary
1171
1208
+------------+----------+---------+--------+----------+-----------+-----------+
1172
1209
| Scala | [] | T <: X | T >: X | | use, decl | +, - |
1173
1210
+------------+----------+---------+--------+----------+-----------+-----------+
1174
- | Swift | <> | T: X | | | decl | inferred |
1211
+ | Swift | <> | T: X | | | n/a | n/a |
1175
1212
+------------+----------+---------+--------+----------+-----------+-----------+
1176
1213
| Rust | <> | T: X, | | = | decl | inferred, |
1177
1214
| | | where | | | | explicit |
1178
1215
+------------+----------+---------+--------+----------+-----------+-----------+
1179
- | Kotlin | <> | T: X | | | use, decl | inferred |
1216
+ | Kotlin | <> | T: X, | | | use, decl | in, out |
1217
+ | | | where | | | | |
1180
1218
+------------+----------+---------+--------+----------+-----------+-----------+
1181
1219
| Julia | {} | T <: X | X <: T | | n/a | n/a |
1182
1220
+------------+----------+---------+--------+----------+-----------+-----------+
1221
+ | Dart | <> | extends | | | decl | in, out, |
1222
+ | | | | | | | inout |
1223
+ +------------+----------+---------+--------+----------+-----------+-----------+
1183
1224
| Python | [] | T: X | | | decl | inferred |
1184
1225
| (proposed) | | | | | | |
1185
1226
+------------+----------+---------+--------+----------+-----------+-----------+
0 commit comments