Skip to content

Conversation

@ConsoleTVs
Copy link

@ConsoleTVs ConsoleTVs commented Apr 20, 2025

This PR adds a new method CollectRowsInto to allow overriding the slice type returned
by CollectRows. This change is backwards compatible.

type User struct{}
type Users []User

// ...

CollectRows[User] // -> []User (can't override)
CollectRowsInto[Users] // -> Users (implicit)
CollectRowsInto[Users, User] // -> Users (explicit)

@ConsoleTVs ConsoleTVs changed the title feat: better collect rows generic parameters Better CollectRows generic parameters Apr 20, 2025
@ConsoleTVs ConsoleTVs changed the title Better CollectRows generic parameters Add CollectRowsInto to allow custom slice types Apr 20, 2025
@jackc
Copy link
Owner

jackc commented Apr 26, 2025

Do I understand correctly that this is to save a type cast or is it doing more? Without tests its a bit hard to follow the intent.

@ConsoleTVs
Copy link
Author

Oh yes, it's pure type gimmicks, there's no new logic involved.

@jackc
Copy link
Owner

jackc commented May 3, 2025

As far as I can tell you can do the same by assigning the result of CollectRows with = to a variable of your desired type rather than using := to create it. It saves one line of code (or two if err needs to be declared).

I'm somewhat inclined against adding a public function to save a line of code on the basis of avoiding the increase in API surface unless it was going to be very frequently used. And anyone that is doing that type operation frequently can add this helper function to their project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants