On Fri, Feb 17, 2023 at 4:47 PM Jochen Voss <jochen.v...@gmail.com> wrote:,
>
> I would like to write a  type constraint which matches types like the 
> following:
>
> type v1 int
>
> func (obj v1) Add(other v1) v1 {
>     return obj + other
> }
>
> type v2 string
>
> func (obj v2) Add(other v2) v2 {
>     return obj + other
> }
>
> This should match anything with an Add() method which can add another item of 
> the same type.  I want to be able to write a function like the following:
>
> func Double[Adder ...???...](x Adder) Adder {
>     return x.Add(x)
> }
>
> My best attempts is https://go.dev/play/p/R0WLqSq9dys , but this doesn't 
> quite work.  What is the correct type constraint to use?

This is one way to do it: https://go.dev/play/p/uUxkvds5HLB .

Ian

-- 
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/CAOyqgcVnvxWp7A0N9HKAaPmHwmO5qLiQ_4%3DTY%2BAZjs1gjhK4PA%40mail.gmail.com.

Reply via email to