Tutorial: Getting started with generics - The Go Programming Language – Okay @xuu I quite like Go’s generics now 🤣 After going through this myself I like the semantics and the syntax. I’m glad they did a lot of work on this to keep it simple to both understand and use (just like the rest of Go) 👌
Q: Is there anything else to this besides:
- Type parameters
- Type arguments (which can be omitted)
- Type constants and unions types (which can be defined as new interface types)
? 🤔
I also like that they really are called “Type Parameters”, which is what they are, and not some god awful scary C++ bullshit templating garbage 😅
Starting with version 1.18, Go has added support for generics, also known as type parameters.
one that i think is pretty interesting is building up dependent constraints. see here.. it accepts a type but requires the use of a pointer to type.
https://github.com/sour-is/ev/blob/main/pkg/es/es.go#L315-L325
@xuu@txt.sour.is Hmm I don’t think I understand that section of code 😢
@prologic@twtxt.net see where its used maybe that can help.
https://github.com/sour-is/ev/blob/main/app/peerfinder/http.go#L153
This is an upsert. So I pass a streamID which is like a globally unique id for the object. And then see how the type of the parameter in the function is used to infer the generic type. In the function it will create a new *Info and populate it from the datastore to pass to the function. The func will do its modifications and if it returns a nil error it will commit the changes.
The PA type contract ensures that the type fulfills the Aggregate interface and is a pointer to type at compile time.