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
+ The "extends" keyword is used to specify an upper bound. The "super" keyword
900
+ is used to specify contravarriant bound
900
901
901
902
Java uses use-site variance. The compiler places limits on which methods and
902
903
members can be accessed based on the use of a generic type. Variance is
@@ -912,6 +913,8 @@ Java provides no way to specify a default type argument.
912
913
913
914
// Generic method
914
915
public <S extends Number> void method1(S value) { }
916
+ // Use site variance
917
+ public void method1(ClassA<? super Integer> value) { }
915
918
}
916
919
917
920
@@ -1105,7 +1108,8 @@ 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
+ The upper bound of a type is specified using a colon. Alternatively
1112
+ a "where" clause can specify various constraints.
1109
1113
1110
1114
Kotlin supports declaration-site variance where variance of type parameters is
1111
1115
explicitly declared using "in" and "out" keywords. It also supports use-site
@@ -1116,19 +1120,19 @@ Kotlin provides no way to specify a default type argument.
1116
1120
::
1117
1121
1118
1122
// Generic class
1119
- class ClassA<T> { }
1123
+ class ClassA<T>
1120
1124
1121
1125
// Type parameter with upper bound
1122
- class ClassB<T: SomeClass1> { }
1126
+ class ClassB<T : SomeClass1>
1123
1127
1124
1128
// Contravariant and covariant type parameters
1125
1129
class ClassC<in S, out T> { }
1126
1130
1127
1131
// Generic function
1128
- fun func1 <T>() -> T {}
1132
+ fun <T> func1(): T { }
1129
1133
1130
1134
// Generic type alias
1131
- typealias<T> = ClassA<T>
1135
+ typealias TypeAliasFoo <T> = ClassA<T>
1132
1136
1133
1137
1134
1138
Julia
@@ -1151,6 +1155,36 @@ upper and lower bounds on a type.
1151
1155
// Alternate form of generic function
1152
1156
function func2(v::Container{T} where T <: Real)
1153
1157
1158
+ Dart
1159
+ -----
1160
+
1161
+ Dart uses angle brackets to declare type parameters and for specialization.
1162
+ The upper bound of a type is specified using a colon.
1163
+
1164
+ Dart supports declaration-site variance where variance of type parameters is
1165
+ explicitly declared using "in", "out" and "inout" keywords. It does not support
1166
+ use-site variance.
1167
+
1168
+ Dart provides no way to specify a default type argument.
1169
+
1170
+ ::
1171
+
1172
+ // Generic class
1173
+ class ClassA<T> { }
1174
+
1175
+ // Type parameter with upper bound
1176
+ class ClassB<T extends SomeClass1> { }
1177
+
1178
+ // Contravariant and covariant type parameters
1179
+ class ClassC<in S, out T> { }
1180
+
1181
+ // Generic function
1182
+ T func1<T>() { }
1183
+
1184
+ // Generic type alias
1185
+ typedef TypeDefFoo<T> = ClassA<T>;
1186
+
1187
+
1154
1188
1155
1189
Summary
1156
1190
-------
@@ -1162,7 +1196,8 @@ Summary
1162
1196
| C++ | template | n/a | n/a | = | n/a | n/a |
1163
1197
| | <> | | | | | |
1164
1198
+------------+----------+---------+--------+----------+-----------+-----------+
1165
- | Java | <> | extends | | | use | inferred |
1199
+ | Java | <> | extends | | | use | super, |
1200
+ | | | | | | | extends |
1166
1201
+------------+----------+---------+--------+----------+-----------+-----------+
1167
1202
| C# | <> | where | | | decl | in, out |
1168
1203
+------------+----------+---------+--------+----------+-----------+-----------+
@@ -1176,10 +1211,14 @@ Summary
1176
1211
| Rust | <> | T: X, | | = | decl | inferred, |
1177
1212
| | | where | | | | explicit |
1178
1213
+------------+----------+---------+--------+----------+-----------+-----------+
1179
- | Kotlin | <> | T: X | | | use, decl | inferred |
1214
+ | Kotlin | <> | T: X, | | | use, decl | in, out |
1215
+ | | | where | | | | |
1180
1216
+------------+----------+---------+--------+----------+-----------+-----------+
1181
1217
| Julia | {} | T <: X | X <: T | | n/a | n/a |
1182
1218
+------------+----------+---------+--------+----------+-----------+-----------+
1219
+ | Dart | <> | extends | | | decl | in, out, |
1220
+ | | | | | | | inout |
1221
+ +------------+----------+---------+--------+----------+-----------+-----------+
1183
1222
| Python | [] | T: X | | | decl | inferred |
1184
1223
| (proposed) | | | | | | |
1185
1224
+------------+----------+---------+--------+----------+-----------+-----------+
0 commit comments