-
Notifications
You must be signed in to change notification settings - Fork 50
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
node/bindnode: consider adding generic helpers once we can assume Go 1.18 or later #340
Comments
We could also consider hiding the fields in those generic types, and only exposing the data via methods. This would allow us to prevent misuse (e.g. using |
This would be really exciting. The current level of exposure that the user of bindnode has to the details of our hacks to persist map ordering has been a sizable wart, and was previously more or less unavoidable. These new possibilities are awesome! |
I wonder if we might want to hoist those Optional and Nullable wrappers somewhere broader. For example, the gogen package also emits an awful lot of boilerplate "MaybeT" types right now. Then again, I don't immediately know if replicating types of those purpose would make any difference to users; maybe it's fine to do so if it reduces coupling of implementations to a useful degree. |
Worth pointing out that it's also possible upstream Go will define an "optional" or "maybe" type of some sort. Some standard library packages like
Indeed. In my defense, when I designed how we deal with maps, it was always the plan to turn that quasi-generic type to a proper generic type. If we were sure we'd never get generics, I might have gone for a different approach. Another candidate for generics would be typed links, as per #272:
Right now, you can only do |
Bonus round: if we retrofit generics, by allowing users on 1.17 or older to manually construct these types like we do with map structs right now, we could add these helpers in the bindnode package behind a |
Follow-up thought: we should probably not allow chaining optional and nullable on the Go side, like
|
OK, so those |
@rvagg that's correct. The generic helpers for Go 1.18+ would just be for UX; once instantiated, the bindnode APIs would still see the same "flat" types that one can type manually today in Go 1.17, just like we already do for the Keys+Values struct dance for maps. |
For instance, we implement ordered maps in the form of:
These containers are precisely what generics is good at. For instance, here's how a
type OrderedMap[K comparable, V any]
could work: https://gotipplay.golang.org/p/bJ1kRHFNWyRAnother two good candidates are optional and nullable types; they are currently represented as a pointer on the Go side, but pointers can cause allocations and memory locality issues, and inferring the schema from a pointer can't know whether it means optional or nullable. So we could for instance use:
All in all, one can imagine a future where declaring bindnode types is fairly pleasant:
The text was updated successfully, but these errors were encountered: