fosstodon.org is one of the many independent Mastodon servers you can use to participate in the fediverse.
Fosstodon is an invite only Mastodon instance that is open to those who are interested in technology; particularly free & open source software. If you wish to join, contact us for an invite.

Administered by:

Server stats:

10K
active users

Would you folks be interested in a type-safe that uses generics instead of code generation or reflection? I have a few ideas, but I'm not sure if Go really needs yet another ORM.

I failed.

The current generics implementation is quite limited. For something usable, I need covariance, variadic generics, parameterized methods, or tuples as first-class types. The best I could do is to map with reflection query results onto structs but sqlx already can do that.

Despite a failure to do anything good with generics, I found an interesting hack (with reflection and unsafe pointer comparison) when you can directly pass struct fields to be used in queries.

I wonder if that can be useful in practice. I like that you can press "go to references" on the field in IDE and find all queries that fetch the field.

gram

I might actually get something useful out of it. The same reflection on fields allows to make type-safe conditions, which is rare for Go ORMs. In the example below, type checker knows that the field `user.age` is int and requires the second argument also be an int. Finally, a place where generics do fit and help.

My main concern is that while it accounts for 80% use cases, it's not as flexible as unconstrained query builders like squirrel. What if you want to have subexpressions? Joins? Some DB-specific syntax? Server-side functions? SQL is too flexible to reasonably fit it into Go.