top | item 44501557

(no title)

dlock17 | 7 months ago

I didn't realize how important order was to type inference.

Are there any real packages out there using these techniques?

discuss

order

Merovius|7 months ago

> I didn't realize how important order was to type inference.

I was unclear, I'm afraid. You can reorder the type parameters, it just changes which of them you need to specify: https://go.dev/play/p/oDIFl3fZiPl

The point is that you can only leave off elements from the end of the list, to have them automatically inferred.

> Are there any real packages out there using these techniques?

I think so far, the usage of generics for containers in Go is still relatively sparse, in public code. I think in part that is because the documentation of how to do that is relatively sparse. That is part of the motivation for the post, to have a bit of somewhat official documentation for these things, so they become more widely known.

The standard library is just starting to add generic containers: https://github.com/golang/go/issues/69559 And part of that is discussing how we want to do things like this: https://github.com/golang/go/issues/70471

That being said, I have used the pointer receiver thing in my dayjob. One example is protobuf. We have a generic helper to set a protobuf enum from the environment. Because of how the API was designed, that required a pointer receiver constraint.

dlock17|7 months ago

The automatic part was what I was referring to, yes. I didn't realize you wrote the article, thanks!

The article mentions using the function version to implement all others, but also that the method version would be optimized better.

Would the compiler be able to inline MethodTree's compare even though it's passed in as a function variable to node.insert?

cyberax|7 months ago

Like many other people, I tried my hand at a generic container library. It worked, but was surprisingly impractical. For example, debugging was hell - there are no custom type renderers in delve.