Proposal: Generic Methods for Go has been accepted

The proposal for generic methods was accepted:

What do you all think? I’m cautiously optimistic. I’ve been build the Astra DB client for Go and this has ramifications for that project. For example on this “select from table” example I did something Mongo-ish:

table := db.Table(tableName)

// Find the book we inserted in TableInsertOne
var book Book
err := table.FindOne(ctx, filter.Eq("title", "The Great Gatsby")).Decode(&book)

BUT - if I had generic methods, I could do something more like:

table := db.Table[Book](tableName)

// Find the book we inserted in TableInsertOne
book, err := table.FindOne(ctx, filter.Eq("title", "The Great Gatsby"))
// And further interactions with db handle will be of type `Book`...

Anyway, I’ll wait to see how this plays out. But like I said - I am cautiously optimistic and can think of some real-world scenarios where I could use this.

1 Like

Considering who this comes from and what kinds of problems it solves (and people have been asking for this, I believe), cautiously optimistic is where it’s at.

There is a potential ambiguity of the syntax in where db.Table could be something like a map[type]func(string) Table, but types aren’t first class citizens in Go the way they are, say, in Python so perhaps that’s not an issue at all.

In any case, I like the way it looks in the resulting code, unlike iterators – and I wasn’t against them either. :slight_smile:

1 Like

I haven’t used iterators truth be told (just haven’t had a use case for them yet!). But - I have heard that writing them doesn’t feel good but consuming them is great. Also on the Astra DB project I’ve been thinking about offering a version of the cursor that uses iterators to abstract the cursor away from the consumer. So you can just range over the cursor and it is pulling back pages of data behind the scenes and you don’t have to deal with paging yourself.

Only thing I have been running up against there is: DB cursors can fail mid-stream so instead of iter.Seq[T] I’d probably have to do iter.Seq2[T, error] which I’m not sure about. And there is some context funkiness as well.

Anyway, based on this comment:

Can we realistically hope for this to ship with Go 1.27?

No promises, but it’s certainly within the realm of possibility.

The long pole with languages changes is rarely the change to the compiler. It’s the ripple effect on the tooling ecosystem. We’ve gotten better at that over the years, but it’s still pretty hard to predict.

We might see it as soon as 1.27.

1 Like

Regarding DB cursors, you could pass an error handler function to the function that creates the iterator. I did something similar for a wrapper over bufio.Scanner.

Thanks. That’s an interesting idea.

I’m really looking forward to this. In my view not allowing generics on methods was an annoying exception to using generics and made some types I designed more cumbersome. I see no downsides and hope it lands soon.

For the range over cursor Iterator using Seq2 with an error is probably a good idea. There could be two types of error - one error in parsing the individual row (which implies only the row had a failure, but future rows can still be retrieved) and a more general error, which forces the iterator to fail prematurely. The second case could be handled by a specified error-type (ErrUnrecoverable).

2 Likes

Yeah I’m quite excited about this.

In my API framework go-don I have generic handlers and was forced to have an API like this:

r.Get("/greet/:name", don.H(GreetHandler))

Once this lands I’ll be able to accept the generic handlers directly, without the ugly wrapper.

I’ve been waiting for this essentially since generics first dropped!