Re: [go-nuts] Another generics question

2023-02-18 Thread Jochen Voss
Thanks a lot!

As an aside, the playgound is great.  It made this conversation so much 
easier!

All the best,
Jochen
On Saturday, 18 February 2023 at 01:31:16 UTC Ian Lance Taylor wrote:

> On Fri, Feb 17, 2023 at 4:47 PM Jochen Voss  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/7c9f4ac8-6951-44cb-b94f-2cee8751a049n%40googlegroups.com.


Re: [go-nuts] Another generics question

2023-02-17 Thread Ian Lance Taylor
On Fri, Feb 17, 2023 at 4:47 PM Jochen Voss  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.


[go-nuts] Another generics question

2023-02-17 Thread Jochen Voss
Hello,

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?

Many thanks,
Jochen

-- 
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/5d8fcc25-2174-4c1d-a325-29111932fe6dn%40googlegroups.com.