Re: [go-nuts] Can Generics match implementers of an interface?

2023-10-21 Thread tapi...@gmail.com


On Sunday, October 22, 2023 at 4:09:51 AM UTC+8 Mike Schinkel wrote:

@Axel Wagner — 

Thank you for adding clarity to this.

Yes, it would be nice if we could get this to be handled with better type 
inference. 


It is hard to call such type inference better. That is too aggressive.
 


But unless and until then, we have our workarounds.

On Saturday, October 21, 2023 at 4:01:05 PM UTC-4 Axel Wagner wrote:

This is purely a type-inference problem. If you explicitly instantiate 
`Append`, it works: https://goplay.tools/snippet/15RFAPFPl3y
Type inference is still relatively limited. It might become possible to at 
some point omit the instantiation. I think this might be "Inferring based 
on interfaces" in https://github.com/golang/go/issues/58650
Until then, it's expected that there will be some cases where you need to 
specify the types.

On Sat, Oct 21, 2023 at 9:45 PM Mike Schinkel  wrote:

No, that pre-generics case is not sufficient for my use-case.  

For my use-case I need to be able to use other types besides []Suiter such 
as []int, e.g.:

var n []int
n = Append(n, 1)
n = Append(n, 2)
n = Append(n, 3)
n = Append(n, 4)
for _, i := range n {
fmt.Printf("Int: %d\n", i)
}

See full code in playground .
On Saturday, October 21, 2023 at 3:15:03 PM UTC-4 Dan Kortschak wrote:

On Sat, 2023-10-21 at 11:58 -0700, Mike Schinkel wrote: 
> Recently I was trying to write a func using generics where I wanted 
> to use a slice of an interface that would contain implementers of 
> that interface and then pass those types to a generic function, but I 
> ran into this error: 
> 
> type MyStruct of MySlice{} does not match inferred type MyInterface 
> for T 
> 
> My code is complex so I wrote the simplest example for this email and 
> here is part of that code: 
> 
> type Suiter interface { 
> Suit() 
> } 
> func Append[T any](s []T, i T) []T { 
> return append(s, i) 
> } 
> func main() { 
> var s []Suiter 
> 
> //CAN GENERICS SUPPORT THIS? 
> //s = Append(s, Clubs{}) 
> //s = Append(s, Hearts{}) 
> //s = Append(s, Diamonds{}) 
> //s = Append(s, Spades{}) 
> 
> //VERSUS HAVING TO DO THIS? 
> s = Append(s, Suiter(Clubs{})) 
> s = Append(s, Suiter(Hearts{})) 
> s = Append(s, Suiter(Diamonds{})) 
> s = Append(s, Suiter(Spades{})) 
> 
> for _, suit := range s { 
> fmt.Printf("Suit: %s\n", suitName(suit)) 
> } 
> } 
> The full code is here in a playground. 
> 
> Note: My example func Append() makes no sense in real use, I only use 
> it as it should be an easily understood example to show the syntax I 
> am trying to illustrate. 
> 
> Question: Is there a way to write Append() such that I can call 
> Append(s, Clubs{}) instead of having to write Append(s, 
> Suiter(Clubs{}))? 
> 
> And if no, is there a chance that in the future Go generics will be 
> able to support that level of type inference? 
> 

The pre-generics language handles this case: 
https://go.dev/play/p/jaJF7LTSVYe 

Is there something about your real case that makes this not acceptable? 

-- 
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...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/805dbd4a-e2a0-426e-b61f-45b9333803f5n%40googlegroups.com
 

.

-- 
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/3225354d-de49-4f77-b719-2e4665f7963fn%40googlegroups.com.


Re: [go-nuts] Can Generics match implementers of an interface?

2023-10-21 Thread Mike Schinkel
@Axel Wagner — 

Thank you for adding clarity to this.

Yes, it would be nice if we could get this to be handled with better type 
inference. 

But unless and until then, we have our workarounds.

On Saturday, October 21, 2023 at 4:01:05 PM UTC-4 Axel Wagner wrote:

> This is purely a type-inference problem. If you explicitly instantiate 
> `Append`, it works: https://goplay.tools/snippet/15RFAPFPl3y
> Type inference is still relatively limited. It might become possible to at 
> some point omit the instantiation. I think this might be "Inferring based 
> on interfaces" in https://github.com/golang/go/issues/58650
> Until then, it's expected that there will be some cases where you need to 
> specify the types.
>
> On Sat, Oct 21, 2023 at 9:45 PM Mike Schinkel  
> wrote:
>
>> No, that pre-generics case is not sufficient for my use-case.  
>>
>> For my use-case I need to be able to use other types besides []Suiter such 
>> as []int, e.g.:
>>
>> var n []int
>> n = Append(n, 1)
>> n = Append(n, 2)
>> n = Append(n, 3)
>> n = Append(n, 4)
>> for _, i := range n {
>> fmt.Printf("Int: %d\n", i)
>> }
>>
>> See full code in playground .
>> On Saturday, October 21, 2023 at 3:15:03 PM UTC-4 Dan Kortschak wrote:
>>
>>> On Sat, 2023-10-21 at 11:58 -0700, Mike Schinkel wrote: 
>>> > Recently I was trying to write a func using generics where I wanted 
>>> > to use a slice of an interface that would contain implementers of 
>>> > that interface and then pass those types to a generic function, but I 
>>> > ran into this error: 
>>> > 
>>> > type MyStruct of MySlice{} does not match inferred type MyInterface 
>>> > for T 
>>> > 
>>> > My code is complex so I wrote the simplest example for this email and 
>>> > here is part of that code: 
>>> > 
>>> > type Suiter interface { 
>>> > Suit() 
>>> > } 
>>> > func Append[T any](s []T, i T) []T { 
>>> > return append(s, i) 
>>> > } 
>>> > func main() { 
>>> > var s []Suiter 
>>> > 
>>> > //CAN GENERICS SUPPORT THIS? 
>>> > //s = Append(s, Clubs{}) 
>>> > //s = Append(s, Hearts{}) 
>>> > //s = Append(s, Diamonds{}) 
>>> > //s = Append(s, Spades{}) 
>>> > 
>>> > //VERSUS HAVING TO DO THIS? 
>>> > s = Append(s, Suiter(Clubs{})) 
>>> > s = Append(s, Suiter(Hearts{})) 
>>> > s = Append(s, Suiter(Diamonds{})) 
>>> > s = Append(s, Suiter(Spades{})) 
>>> > 
>>> > for _, suit := range s { 
>>> > fmt.Printf("Suit: %s\n", suitName(suit)) 
>>> > } 
>>> > } 
>>> > The full code is here in a playground. 
>>> > 
>>> > Note: My example func Append() makes no sense in real use, I only use 
>>> > it as it should be an easily understood example to show the syntax I 
>>> > am trying to illustrate. 
>>> > 
>>> > Question: Is there a way to write Append() such that I can call 
>>> > Append(s, Clubs{}) instead of having to write Append(s, 
>>> > Suiter(Clubs{}))? 
>>> > 
>>> > And if no, is there a chance that in the future Go generics will be 
>>> > able to support that level of type inference? 
>>> > 
>>>
>>> The pre-generics language handles this case: 
>>> https://go.dev/play/p/jaJF7LTSVYe 
>>>
>>> Is there something about your real case that makes this not acceptable? 
>>>
>>> -- 
>> 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...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/805dbd4a-e2a0-426e-b61f-45b9333803f5n%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
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/adfe6f16-92a5-4195-bcda-2412506c2410n%40googlegroups.com.


Re: [go-nuts] Can Generics match implementers of an interface?

2023-10-21 Thread 'Axel Wagner' via golang-nuts
This is purely a type-inference problem. If you explicitly instantiate
`Append`, it works: https://goplay.tools/snippet/15RFAPFPl3y
Type inference is still relatively limited. It might become possible to at
some point omit the instantiation. I think this might be "Inferring based
on interfaces" in https://github.com/golang/go/issues/58650
Until then, it's expected that there will be some cases where you need to
specify the types.

On Sat, Oct 21, 2023 at 9:45 PM Mike Schinkel  wrote:

> No, that pre-generics case is not sufficient for my use-case.
>
> For my use-case I need to be able to use other types besides []Suiter such
> as []int, e.g.:
>
> var n []int
> n = Append(n, 1)
> n = Append(n, 2)
> n = Append(n, 3)
> n = Append(n, 4)
> for _, i := range n {
> fmt.Printf("Int: %d\n", i)
> }
>
> See full code in playground .
> On Saturday, October 21, 2023 at 3:15:03 PM UTC-4 Dan Kortschak wrote:
>
>> On Sat, 2023-10-21 at 11:58 -0700, Mike Schinkel wrote:
>> > Recently I was trying to write a func using generics where I wanted
>> > to use a slice of an interface that would contain implementers of
>> > that interface and then pass those types to a generic function, but I
>> > ran into this error:
>> >
>> > type MyStruct of MySlice{} does not match inferred type MyInterface
>> > for T
>> >
>> > My code is complex so I wrote the simplest example for this email and
>> > here is part of that code:
>> >
>> > type Suiter interface {
>> > Suit()
>> > }
>> > func Append[T any](s []T, i T) []T {
>> > return append(s, i)
>> > }
>> > func main() {
>> > var s []Suiter
>> >
>> > //CAN GENERICS SUPPORT THIS?
>> > //s = Append(s, Clubs{})
>> > //s = Append(s, Hearts{})
>> > //s = Append(s, Diamonds{})
>> > //s = Append(s, Spades{})
>> >
>> > //VERSUS HAVING TO DO THIS?
>> > s = Append(s, Suiter(Clubs{}))
>> > s = Append(s, Suiter(Hearts{}))
>> > s = Append(s, Suiter(Diamonds{}))
>> > s = Append(s, Suiter(Spades{}))
>> >
>> > for _, suit := range s {
>> > fmt.Printf("Suit: %s\n", suitName(suit))
>> > }
>> > }
>> > The full code is here in a playground.
>> >
>> > Note: My example func Append() makes no sense in real use, I only use
>> > it as it should be an easily understood example to show the syntax I
>> > am trying to illustrate.
>> >
>> > Question: Is there a way to write Append() such that I can call
>> > Append(s, Clubs{}) instead of having to write Append(s,
>> > Suiter(Clubs{}))?
>> >
>> > And if no, is there a chance that in the future Go generics will be
>> > able to support that level of type inference?
>> >
>>
>> The pre-generics language handles this case:
>> https://go.dev/play/p/jaJF7LTSVYe
>>
>> Is there something about your real case that makes this not acceptable?
>>
>> --
> 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/805dbd4a-e2a0-426e-b61f-45b9333803f5n%40googlegroups.com
> 
> .
>

-- 
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/CAEkBMfEEBQsMR6RfHMw3nHvr7peq-Mu05X0M55XqZ%3DERVQ5Sbg%40mail.gmail.com.


Re: [go-nuts] Can Generics match implementers of an interface?

2023-10-21 Thread Mike Schinkel
No, that pre-generics case is not sufficient for my use-case.  

For my use-case I need to be able to use other types besides []Suiter such 
as []int, e.g.:

var n []int
n = Append(n, 1)
n = Append(n, 2)
n = Append(n, 3)
n = Append(n, 4)
for _, i := range n {
fmt.Printf("Int: %d\n", i)
}

See full code in playground .
On Saturday, October 21, 2023 at 3:15:03 PM UTC-4 Dan Kortschak wrote:

> On Sat, 2023-10-21 at 11:58 -0700, Mike Schinkel wrote:
> > Recently I was trying to write a func using generics where I wanted
> > to use a slice of an interface that would contain implementers of
> > that interface and then pass those types to a generic function, but I
> > ran into this error:
> > 
> > type MyStruct of MySlice{} does not match inferred type MyInterface
> > for T
> > 
> > My code is complex so I wrote the simplest example for this email and
> > here is part of that code:
> > 
> > type Suiter interface {
> > Suit()
> > }
> > func Append[T any](s []T, i T) []T {
> > return append(s, i)
> > }
> > func main() {
> > var s []Suiter
> > 
> > //CAN GENERICS SUPPORT THIS?
> > //s = Append(s, Clubs{})
> > //s = Append(s, Hearts{})
> > //s = Append(s, Diamonds{})
> > //s = Append(s, Spades{})
> > 
> > //VERSUS HAVING TO DO THIS?
> > s = Append(s, Suiter(Clubs{}))
> > s = Append(s, Suiter(Hearts{}))
> > s = Append(s, Suiter(Diamonds{}))
> > s = Append(s, Suiter(Spades{}))
> > 
> > for _, suit := range s {
> > fmt.Printf("Suit: %s\n", suitName(suit))
> > }
> > }
> > The full code is here in a playground.
> > 
> > Note: My example func Append() makes no sense in real use, I only use
> > it as it should be an easily understood example to show the syntax I
> > am trying to illustrate.
> > 
> > Question: Is there a way to write Append() such that I can call
> > Append(s, Clubs{}) instead of having to write Append(s,
> > Suiter(Clubs{}))?
> > 
> > And if no, is there a chance that in the future Go generics will be
> > able to support that level of type inference?
> > 
>
> The pre-generics language handles this case:
> https://go.dev/play/p/jaJF7LTSVYe
>
> Is there something about your real case that makes this not acceptable?
>
>

-- 
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/805dbd4a-e2a0-426e-b61f-45b9333803f5n%40googlegroups.com.


Re: [go-nuts] Can Generics match implementers of an interface?

2023-10-21 Thread 'Dan Kortschak' via golang-nuts
On Sat, 2023-10-21 at 11:58 -0700, Mike Schinkel wrote:
> Recently I was trying to write a func using generics where I wanted
> to use a slice of an interface that would contain implementers of
> that interface and then pass those types to a generic function, but I
> ran into this error:
> 
> type MyStruct of MySlice{} does not match inferred type MyInterface
> for T
> 
> My code is complex so I wrote the simplest example for this email and
> here is part of that code:
> 
> type Suiter interface {
> Suit()
> }
> func Append[T any](s []T, i T) []T {
> return append(s, i)
> }
> func main() {
> var s []Suiter
> 
> //CAN GENERICS SUPPORT THIS?
> //s = Append(s, Clubs{})
> //s = Append(s, Hearts{})
> //s = Append(s, Diamonds{})
> //s = Append(s, Spades{})
> 
> //VERSUS HAVING TO DO THIS?
> s = Append(s, Suiter(Clubs{}))
> s = Append(s, Suiter(Hearts{}))
> s = Append(s, Suiter(Diamonds{}))
> s = Append(s, Suiter(Spades{}))
> 
> for _, suit := range s {
> fmt.Printf("Suit: %s\n", suitName(suit))
> }
> }
> The full code is here in a playground.
> 
> Note: My example func Append() makes no sense in real use, I only use
> it as it should be an easily understood example to show the syntax I
> am trying to illustrate.
> 
> Question: Is there a way to write Append() such that I can call
> Append(s, Clubs{}) instead of having to write Append(s,
> Suiter(Clubs{}))?
> 
> And if no, is there a chance that in the future Go generics will be
> able to support that level of type inference?
> 

The pre-generics language handles this case:
https://go.dev/play/p/jaJF7LTSVYe

Is there something about your real case that makes this not acceptable?

-- 
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/9aab12f4935edebe2067bbae3c00faa5b50c79d8.camel%40kortschak.io.


[go-nuts] Can Generics match implementers of an interface?

2023-10-21 Thread Mike Schinkel
Recently I was trying to write a func using generics where I wanted to use 
a slice of an interface that would contain implementers of that interface 
and then pass those types to a generic function, but I ran into this error:

type MyStruct of MySlice{} does not match inferred type MyInterface for T

My code is complex so I wrote the simplest example for this email and here 
is part of that code:

type Suiter interface {
Suit()
}
func Append[T any](s []T, i T) []T {
return append(s, i)
}
func main() {
var s []Suiter

//CAN GENERICS SUPPORT THIS?
//s = Append(s, Clubs{})
//s = Append(s, Hearts{})
//s = Append(s, Diamonds{})
//s = Append(s, Spades{})

//VERSUS HAVING TO DO THIS?
s = Append(s, Suiter(Clubs{}))
s = Append(s, Suiter(Hearts{}))
s = Append(s, Suiter(Diamonds{}))
s = Append(s, Suiter(Spades{}))

for _, suit := range s {
fmt.Printf("Suit: %s\n", suitName(suit))
}
}
The full code is here  in a 
playground.

*Note: *My example func Append() makes no sense in real use, I only use it 
as it should be an easily understood example to show the syntax I am trying 
to illustrate.

*Question:* Is there a way to write Append() such that I can call Append(s, 
Clubs{}) instead of having to write Append(s, Suiter(Clubs{}))?

And if no, is there a chance that in the future Go generics will be able to 
support that level of type inference?

-- 
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/1b01e1d8-0814-4a77-a67a-399f52da1ff4n%40googlegroups.com.