4 ms·
I think there may be confusion in language here. At no point am I asking about doing something that would reduce type safety. Supposing I have a function that
by orblivion 5y ago
I think there may be confusion in language here. At no point am I asking about doing something that would reduce type safety.
Supposing I have a function that takes [A any] and returns [A any]. As I use the function, it is not returning type "any" aka "interface{}", aka pointer to unspecified type (which has always been possible in Go). It's returning type A, which is specified at compile time, where A can be any type, but has to be the same type passed into the function.
Supposing I have a function that takes [B io.Reader] and returns [B io.Reader]. Again, it doesn't return io.Reader, which again was always possible. Returning type io.Reader would be returning a pointer to unspecified type that implements the io.Reader interface. That sounds like what you thought I was saying. What I'm actually talking about is returning type B, specified at compile time, where B is restricted to types that implement io.Reader, and is also the same type passed into the function.
In both cases, you're making a function that takes a type and returns the same type. In the second case, that type can only be one that implements the io.Reader interface.
- jayd16 5y agoExactly right. Now returning to your question... Is it not clear that you might want a method with this type safety that is also restricted to something more specific than [A any]?
- orblivion 5y agoI do want that. My question is why the blog post doesn't have any examples of it, other than in the "not recommended" section. I'm curious what sorts of uses the Go team would recommend (EDIT: and which they would discourage. I know I'd find uses if left to my own devices, but the Go team might discourage some of them.) So, you could see this as a recommendation/request for a followup blog post.
- orblivion 5y agoSince you say you don't know Go, I'll clarify one thing: the Go ethos includes curbing programmers who like to be clever and tricky and sophisticated. It irritates a lot of us but I understand there's probably wisdom in it. Haskell is one of my favorite languages, you don't have to sell me on generics. Now that Go relented and finally implemented them, a lot of people are worried that the "clever" programmers are going to have their way. This blog post, I think, is a means to curb that. So I'm wondering exactly where the line is from their perspective.