As a note, this is how I solve this problem without generics:

https://go.dev/play/p/nsDca0McADY

On Wednesday, July 27, 2022 at 2:54:20 PM UTC-7 John wrote:

> With 1.18 generics, I'm curious if anyone has a good solution using 
> generics to solve the optional argument that can be used in multiple method 
> problem.
>
> So say I have two methods, .A() and .B().  Both accept optional arguments 
> where some optional arguments might be shared.
>
> Here is a very loose (do not take any of this as the solution, just trying 
> to show what I'm kinda trying to achieve). 
>
> type AOptions struct{
>   Shared Opt string
>   NotSharedOpt bool
> }
>
> type BOptions struct{
>   Shared Opt string
>   NotSharedOpt int
> }
>
> func WithShared(shared string) CallOption {
>   ...
> }
>
> func WithANotShared(b bool) CallOption {
>   ...
> }
>
> func (c Client) A(options ...CallOption) {
>   opts := AOptions{}
>   for _, o := range options {
>     o(&opts)
>   }
>   ... 
> }
> func (c Client B(options ...CalOption) {
>   ...
> }
>
> We want to be able to use WithShared() with both A() and B(), but only  
> WithANotShared with the A() method.  
>
> I've had different solutions to this problem that would give runtime 
> errors, but I'm curious with generics if there is one for compile time that 
> anyone has come up with.
>
> Cheers.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/61967fec-929a-4865-80ff-14ab25a1d39dn%40googlegroups.com.

Reply via email to