Skip to content

Commit a8a1cc4

Browse files
updating generics information
1 parent 69ade8d commit a8a1cc4

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

topics/go/generics/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@ There is a [go2go](https://go2goplay.golang.org/) playground that will allow you
88

99
This comes from the draft document and provides a nice overview of what generics support is being worked on for the first potential release.
1010

11-
* Functions can have an additional type parameter list introduced by the keyword type: func F(type T)(p T) { ... }.
11+
* Functions can have an additional type parameter list that uses square brackets but otherwise looks like an ordinary parameter list: func F[T any](p T) { ... }.
1212
* These type parameters can be used by the regular parameters and in the function body.
13-
* Types can also have a type parameter list: type M(type T) []T.
14-
* Each type parameter can have an optional type constraint: func F(type T Constraint)(p T) { ... }
13+
* Types can also have a type parameter list: type M[T any] []T.
14+
* Each type parameter has a type constraint, just as each ordinary parameter has a type: func F[T Constraint](p T) { ... }.
1515
* Type constraints are interface types.
16-
* Interface types used as type constraints can have a list of predeclared types; only types whose underlying type is one of those types can implement the interface.
17-
* Using a generic function or type requires passing type arguments.
18-
* Type inference permits omitting the type arguments in common cases.
19-
* If a type parameter has a type constraint its type argument must implement the interface.
16+
* The new predeclared name any is a type constraint that permits any type.
17+
* Interface types used as type constraints can have a list of predeclared types; only type arguments that match one of those types satisfy the constraint.
2018
* Generic functions may only use operations permitted by the type constraint.
19+
* Using a generic function or type requires passing type arguments.
20+
* Type inference permits omitting the type arguments of a function call in common cases.
2121

2222
## Omissions
2323

2424
This comes from the draft document and provides a nice overview of what generics support is NOT being worked on for the first potential release.
2525

2626
* No specialization. There is no way to write multiple versions of a generic function that are designed to work with specific type arguments.
2727
* No metaprogramming. There is no way to write code that is executed at compile time to generate code to be executed at run time.
28-
* No higher level abstraction. There is no way to speak about a function with type arguments other than to call it or instantiate it. There is no way to speak about a generic type other than to instantiate it.
28+
* No higher level abstraction. There is no way to use a function with type arguments other than to call it or instantiate it. There is no way to use a generic type other than to instantiate it.
2929
* No general type description. In order to use operators in a generic function, constraints list specific types, rather than describing the characteristics that a type must have. This is easy to understand but may be limiting at times.
3030
* No covariance or contravariance of function parameters.
3131
* No operator methods. You can write a generic container that is compile-time type-safe, but you can only access it with ordinary methods, not with syntax like c[k].
32-
* No currying. There is no way to specify only some of the type arguments, other than by using a helper function or a wrapper type.
32+
* No currying. There is no way to partially instantiate a generic function or type, other than by using a helper function or a wrapper type. All type arguments must be either explicitly passed or inferred at instantiation time.
3333
* No variadic type parameters. There is no support for variadic type parameters, which would permit writing a single generic function that takes different numbers of both type parameters and regular parameters.
3434
* No adaptors. There is no way for a constraint to define adaptors that could be used to support type arguments that do not already implement the constraint, such as, for example, defining an == operator in terms of an Equal method, or vice-versa.
35-
* No parameterization on non-type values such as constants. This arises most obviously for arrays, where it might sometimes be convenient to write type Matrix(type n int) [n][n]float64. It might also sometimes be useful to specify significant values for a container type, such as a default value for elements.
35+
* No parameterization on non-type values such as constants. This arises most obviously for arrays, where it might sometimes be convenient to write type Matrix[n int] [n][n]float64. It might also sometimes be useful to specify significant values for a container type, such as a default value for elements.
3636

3737
## Posts About This Code
3838

0 commit comments

Comments
 (0)