-
Notifications
You must be signed in to change notification settings - Fork 76
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
add utilities for applying options and copying protobufs #809
Conversation
|
|
||
package options | ||
|
||
func Apply[T any, F ~func(T)](o T, opts []F) T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry Paul, I already asked you this once, but I forgot the answer :-(. What does the ~
mean there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
~
means the type constraint permits any type with the same underlying type. so for example
type Foo func()
var f0 = func() {}
var f1 Foo = func() {}
func a[T ~func()](f T) {}
func b[T func()](f T) {}
a(f0) // ok
b(f0) // ok
a(f1) // ok
b(f1) // not ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, got it. Thank you Paul. Let me commit this to my poor memory and hope it survives till the next time I see this pattern :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is needed here because we have functional options like
type FooOption func(x *FooOptions)
// several implementations of FooOption
func SomeFoo(opts ...FooOption) {
_ := options.Apply(&FooOptions{}, opts)
// without ~ this wouldn't work because []FooOption is not []func(*FooOptions)
}
import "google.golang.org/protobuf/proto" | ||
|
||
func CloneProto[T proto.Message](m T) T { | ||
return proto.Clone(m).(T) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!!!
No description provided.