-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: database/sql: Support for generics with NullTypes Interface #56439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
what does the interface enable? why can't you define the interface in the code that's using it? |
This proposal has been added to the active column of the proposals project |
It's still unclear what this would enable us to do. |
The only reason for adding this is because the sql package defines all the null types. As it's generic code in the software using the SQL package, it would be better if the interface is defined in the SQL package because if the SQL package adds another type in the future then the generic will not work unless the interface defined in the software using the SQL package updates their interface. So if someone doesn't want to support all the types in their generic code then they define their own interface. But if, as I have, the written code works with all types then it would be nice, if a future release of the sql package added the new type to a published interface instead of the current risk that something breaks. |
@timdadd I don't understand how you would use the interface. Do you have concrete code you can share that would make use of this interface? |
@rsc I have to unmarshall json messages that have an unknown structure but follow the sql.null format because they were marshalled from SQL structs. Being SQL null they are of the format xxx/valid. I want to save writing code for each SQL Null type, so instead I build the functions using a single generic function. Here is my generic function that builds 3 real functions
The calling code is like this:
If an interface exists then I can use instead of [] and then if you add a new type then my code just needs recompilation. Here's a bit of JSON I have to unmarshall, but I can't use a struct because the JSON structure is inconsistent:
Of course you might have a much better way of doing all this, if so then please let me know. |
I don't see anything in interfaceToType that is specific to SQL. It seems like a shorter function would be:
and then used as
This wouldn't need the null interface constraint at all. |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
To support handling of generics, add an interface that includes all the null types
type NullTypes interface { NullString | NullInt64 | NullTime | NullBool ... }
The text was updated successfully, but these errors were encountered: