News for hackers. Stuff that matters.
Re:My real life use-case for generics in Go: API for client-... by Traubenfuchs on Thursday May 19, 2022 @07:33AM (#31433151)
Do you know how Go handled collection classes in the past without generics?

Where they just not typesafe, like a Java List<Object>?

Re:My real life use-case for generics in Go: API for client-... by christophilus on Thursday May 19, 2022 @07:40AM (#31433209)
The native collections were generic already. They were a special case. Custom collections were either hand-rolled for a specific type, unsafe as in your example, or generated per type using a tool.
Re:My real life use-case for generics in Go: API for client-... by feffe on Thursday May 19, 2022 @07:53AM (#31433318)
Built-in map and vector types are generic.

E.g. map[int]*MyType

Using interface{} is quite common in the absence of sum types (or inheritance with base class). I guess interface{} is like Object in Java.

It's not worse than Python or other dynamic languages that many people are fine with. The run-time panics when asserting that a type is something it's not.

E.g. https://go.dev/play/p/c4hx8HSiB8I

That said, no, it's not optimal and could be better (IMHO).

Re:My real life use-case for generics in Go: API for client-... by foldr on Thursday May 19, 2022 @08:30AM (#31433677)
>Built-in map and vector types are generic.

I understand what people mean by this, but it's not true for any reasonable notion of 'generic'. Pre-generics Go no more has generic maps than C has generic arrays. Yes, you can declare a map or a slice of values of any given type, but you can't write a generic function that abstracts over these types. That's what is usually meant by 'generics'.

Re:My real life use-case for generics in Go: API for client-... by maxiepoo on Thursday May 19, 2022 @09:53AM (#31434664)
Sure but it's worth emphasizing that Go language designers gave these built-in collections the exorbitant privilege of being parameterized by a type and declaring that any new type created by a mere programmer has to be monomorphic.
Re:My real life use-case for generics in Go: API for client-... by foldr on Thursday May 19, 2022 @09:56AM (#31434702)
> the exorbitant privilege of being parameterized by a type

This is how built in collection types work in pretty much all statically typed languages. For example, here is an array of ints in C:

    int foo[] = { 1, 2, 3 };
We wouldn't usually say that C has 'generic arrays'.

> any new type created by a mere programmer has to be monomorphic.

Collections in (pre-Generics) Go are monomorphic. []foo and []bar are as different as foo and bar. That is why, for example, the first argument of sort.Slice has type 'any' (https://cs.opensource.google/go/go/+/go1.18.2:src/sort/slice...)

Re:My real life use-case for generics in Go: API for client-... by feffe on Thursday May 19, 2022 @10:41AM (#31435283)
Yes, you are correct. I had not thought about it deeply. There are restrictions for what can be used as a map keys according to some built-in equality rules. And of course the types used as map keys and values are concrete types so you can't pass a map to generic functions pre generics.

But was the built-in append function generic pre generics? I think it is?

Re:My real life use-case for generics in Go: API for client-... by erik_seaberg on Thursday May 19, 2022 @08:19PM (#31441908)
There were three or four generic types and a few generic functions (close, len, cap, make, append, delete, x[y], <-, ==, for-range, select), they just didn’t let normal users define new ones without forking the compiler.
Re:My real life use-case for generics in Go: API for client-... by foldr on Friday May 20, 2022 @04:11AM (#31444643)
I don’t think it makes sense to see built in operations on arrays and maps as generic functions. If 'len' was some kind of built-in generic function then you'd expect to be able to monomorphize it like this:

    func intLen(foo []int, lenfunc func ([]int) int) int {
      return lenfunc(foo)
    }

    intLen([]int{1,2,3}, len)
There is just no sense in which Go 1.17 has “generic types” and (e.g.) C doesn’t. All statically typed languages allow you to define arrays of different types and use the same operators on these arrays. Go adds a handful of additional built in types (slices, maps, channels).

The idea that some completely ordinary language features somehow constitute a crippled version of generics that has been 'turned off' for regular users seems to be an artefact of certain communities' hobbyhorsing over Go's (former) lack of generics.

Re:My real life use-case for generics in Go: API for client-... by erik_seaberg on Saturday May 21, 2022 @07:17PM (#31463103)
As a thing that takes an argument and returns a result, I would have expected len to be an ordinary function. That I don’t is only because I remember the disappointing state of the language at launch.
Re:My real life use-case for generics in Go: API for client-... by KarlKode on Thursday May 19, 2022 @08:30AM (#31433681)
The usual pattern was to use an empty interface (`var collection []interface{}`).