Skip to content

Commit 2fbe588

Browse files
committed
PEP 659: Add Go
1 parent a7b84dd commit 2fbe588

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

pep-0695.rst

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,7 @@ Java provides no way to specify a default type argument.
914914
915915
// Generic method
916916
public <S extends Number> void method1(S value) { }
917+
917918
// Use site variance
918919
public void method1(ClassA<? super Integer> value) { }
919920
}
@@ -1133,7 +1134,12 @@ Kotlin provides no way to specify a default type argument.
11331134
class ClassC<in S, out T>
11341135
11351136
// Generic function
1136-
fun <T> func1(): T { }
1137+
fun <T> func1(): T {
1138+
1139+
// Use site variance
1140+
val covariantA: ClassA<out Number>
1141+
val contravariantA: ClassA<in Number>
1142+
}
11371143
11381144
// Generic type alias
11391145
typealias TypeAliasFoo<T> = ClassA<T>
@@ -1189,6 +1195,31 @@ Dart provides no way to specify a default type argument.
11891195
// Generic type alias
11901196
typedef TypeDefFoo<T> = ClassA<T>;
11911197
1198+
Go
1199+
--
1200+
1201+
Go uses square brackets to declare type parameters and for specialization.
1202+
The upper bound of a type is specified after the name of the parameter, and
1203+
must always be specified. The keyword ``any`` is used for an unbound type parameter.
1204+
1205+
Go doesn't support variance; all type parameters are invariant.
1206+
1207+
Go provides no way to specify a default type argument.
1208+
1209+
Go does not support generic type aliases.
1210+
1211+
.. code-block:: go
1212+
1213+
// Generic type without a bound
1214+
type TypeA[T any] struct {
1215+
t T
1216+
}
1217+
1218+
// Type parameter with upper bound
1219+
type TypeB[T SomeType1] struct { }
1220+
1221+
// Generic function
1222+
func func1[T any]() { }
11921223
11931224
11941225
Summary
@@ -1224,6 +1255,8 @@ Summary
12241255
| Dart | <> | extends | | | decl | in, out, |
12251256
| | | | | | | inout |
12261257
+------------+----------+---------+--------+----------+-----------+-----------+
1258+
| Go | [] | T X | | | n/a | n/a |
1259+
+------------+----------+---------+--------+----------+-----------+-----------+
12271260
| Python | [] | T: X | | | decl | inferred |
12281261
| (proposed) | | | | | | |
12291262
+------------+----------+---------+--------+----------+-----------+-----------+

0 commit comments

Comments
 (0)