Skip to content
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

consider using generics to aid the constructor magic #1210

Closed
marten-seemann opened this issue Sep 29, 2021 · 0 comments · Fixed by #1858
Closed

consider using generics to aid the constructor magic #1210

marten-seemann opened this issue Sep 29, 2021 · 0 comments · Fixed by #1858
Labels
effort/days Estimated to take multiple days, but less than a week exp/intermediate Prior experience is likely helpful kind/enhancement A net-new feature or improvement to an existing feature status/blocked Unable to be worked further until needs are met

Comments

@marten-seemann
Copy link
Contributor

I think the design makes sense. My only worry as a user is that opts ...interface{} is very unsafe at a static level, but I don't think you can do any better without generics. Once you had generics, you could do funky stuff like:

package libp2p

type constructor[T any] func() *T

type option[T any] func(*T)

func Transport[T any, TConstructor constructor[T], TOption option[T]] func(ct TConstructor, opts ...TOption) Option

Note that this would not support arbitrary parameters before the variadic options, like the upgrader in func NewTCPTransport(upgrader *tptu.Upgrader, opts ...Option). I don't think 1.18 generics has support for "arbitrary parameters" like that. But perhaps those could be redesigned to fit generics, to perhaps even remove the need for reflection.

Very much getting off-topic here. Just food for thought, in case you considered generics :)

Originally posted by @mvdan in #1205 (review)

@marten-seemann marten-seemann added effort/days Estimated to take multiple days, but less than a week exp/intermediate Prior experience is likely helpful kind/enhancement A net-new feature or improvement to an existing feature status/blocked Unable to be worked further until needs are met labels Sep 29, 2021
@marten-seemann marten-seemann changed the title consider using Generics to aid the constructor magic consider using generics to aid the constructor magic Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort/days Estimated to take multiple days, but less than a week exp/intermediate Prior experience is likely helpful kind/enhancement A net-new feature or improvement to an existing feature status/blocked Unable to be worked further until needs are met
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant