Unhappy with the official generics tutorial

136 views
Skip to first unread message

Carla Pfaff

unread,
Feb 22, 2024, 3:01:22 AMFeb 22
to golang-nuts
The Go documentation, available at https://go.dev/doc/, features only one tutorial dedicated to generics, found at https://go.dev/doc/tutorial/generics. This tutorial lacks any examples employing the 'any' constraint, nor does it mention it. Instead, it begins with the use of an 'int64 | float64' constraint. These A|B constraints represent a more specialized application of generics, aimed at scenarios where there is a need to utilize an operator such as "+". However, the most universal, adaptable, and reusable form of a generic type or function — one that utilizes 'any' — is notably absent.

Kurtis Rader

unread,
Feb 22, 2024, 3:10:30 AMFeb 22
to Carla Pfaff, golang-nuts
I don't understand your feedback. How, exactly, should generics handle the `any` type? Documentation can always be improved but how would a generic function that accepts two `any` types perform addition of those "any" values?

On Thu, Feb 22, 2024 at 12:01 AM 'Carla Pfaff' via golang-nuts <golan...@googlegroups.com> wrote:
The Go documentation, available at https://go.dev/doc/, features only one tutorial dedicated to generics, found at https://go.dev/doc/tutorial/generics. This tutorial lacks any examples employing the 'any' constraint, nor does it mention it. Instead, it begins with the use of an 'int64 | float64' constraint. These A|B constraints represent a more specialized application of generics, aimed at scenarios where there is a need to utilize an operator such as "+". However, the most universal, adaptable, and reusable form of a generic type or function — one that utilizes 'any' — is notably absent.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0447f6d6-6fe9-410f-9d5a-08bb20bba67en%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Carla Pfaff

unread,
Feb 22, 2024, 4:06:56 AMFeb 22
to golang-nuts
The feedback was not specific to the "SumIntsOrFloats" example in the tutorial. The tutorial fails to demonstrate any generic data structures utilizing the "[T any]" constraint or a function with an 'any' constraint, such as "slices.Clone[S ~[]E, E any](s S) S". This omission is notable considering "any" is among the most frequently used constraints in writing generic code.

Jan Mercl

unread,
Feb 22, 2024, 4:19:58 AMFeb 22
to Carla Pfaff, golang-nuts
On Thu, Feb 22, 2024 at 10:06 AM 'Carla Pfaff' via golang-nuts
<golan...@googlegroups.com> wrote:

> This omission is notable considering "any" is among the most frequently used constraints in writing generic code.

Interesting to know, I'd naively guess the opposite. Can you please
share the source data set? Thank you.

Carla Pfaff

unread,
Feb 22, 2024, 4:38:25 AMFeb 22
to golang-nuts
In the standard library, the most notable packages that add generic functions are slices and maps. Almost all of their functions feature an 'any' constraint. Same for other generic additions to the standard library such as atomic.Pointer, reflect.TypeFor, and sync.OnceValue.

Carla Pfaff

unread,
Feb 22, 2024, 4:39:11 AMFeb 22
to golang-nuts
The tutorial could begin with something simple and general, such as:

func PrintAll[T any](s []T) {
for x := range s {
fmt.Println(x)
}
}

or a function motivated by https://go.dev/doc/faq#convert_slice_of_interface such as:

func Convert[T any](s []T) []any {
r := make([]any, len(s))
for i, v := range s {
r[i] = v
}
return r
}

and then progressively introduce examples with more restrictive constraints as specific methods or operators become necessary.

Instead, it adopts a bottom-up approach, beginning with extremely specific constraints and then broadening the type sets, without culminating in examples that utilize the most general constraint, 'any'.

This situation often leads to many developers beginning their journey with generics using [T A|B], mistakenly treating it as sum types. Consequently, they resort to type switches, resulting in dissatisfaction and a call for the resolution of https://github.com/golang/go/issues/45380.
Reply all
Reply to author
Forward
0 new messages